Throw an exception in doInBackground and catch onPostExecute

I have an error for doInBackgroundmy method Asynctask. Is it possible? how can i make the exception get caught onPostExecute?

+4
source share
3 answers

You cannot throw exceptions on threads.

However, you can catch the exception in doInBackground(), save it somewhere, such as a member variable in asynctask, and then handle it in onPostExecute().

+7
source

No, you cannot throw an exception in the background thread.

, ( , ), onPostExecute(), .

+1

:

  • AsyncTask onPostExecute(), laalto . . AsyncTask , .

  • Object , doInBackground(), onPostExecute(), . , FutureTask ( AsyncTask ) get(), , .

  • Create a holder class for your result, which can be designed to contain either the result or some specific exceptions, define its get method to return the result type, if available, and exclude the exception otherwise, and then set the AsyncTasksubclass to a common type result for this class and catch exceptions to the method onPostExecute()when receiving the result from the holder.

0
source

All Articles