According to the official documentation, AsyncTask#get() throws an ExecutionException when an exception occurs in the threaded Task. How can I notify the current AsyncTask that an exception has been thrown in the AsyncTask#doInBackground(Params...) method?
2 Answers
The best would be not to use AsyncTask.get()
at all because get()
is synchronous so you will gain nothing

- 47,782
- 38
- 107
- 158
-
The thing is I have an AsyncTaskManager object that takes AsyncTasks and manages them notifiying the caller Activity as soon as an AsyncTask finishes. It also offers some other functionality that I need. Given that, I need to be able to call the get() method when an AsyncTask finished. For now, I'll make a workaround, but I think that part of the API wasn't quite well implemented (unless someone actually knows how to make it work properly). – Gonzalo Nov 04 '12 at 18:27
-
Maybe you could use an Executor submitting Runnables/Callables to it instead of AsyncTask. – Alexander Kulyakhtin Nov 04 '12 at 18:31
You can't.
The ExecutionException is thrown by the Future#get()
method that is internally called by AsyncTask#get()
.
Futures do allow to throw Exceptions within their executed asynchronous block. A call to Future#get()
will throw such an Exception encapsulated in an ExecutionException.
AsyncTasks, however, do not allow to throw Exceptions within the AsyncTask#doInBackground()
method. Therefore, you can't use this mechanism for your exception handling. This seems to be an architectural design decision.
Disclaimer:
You can probably still throw RuntimeExceptions in your AsyncTask#doInBackground()
, leading to a related ExecutionException
on AsyncTask#get()
... but you shouldn't ;)
Note:
Are you sure that you want to call AsyncTask#get()
and block your calling thread? Calling it from your main/ui thread will cause your user interface to stop being refreshed until your task is finished.

- 4,634
- 5
- 22
- 25
-
I'm currently using this solution for managing AsyncTasks: http://www.codeproject.com/Articles/162201/Painless-AsyncTask-and-ProgressDialog-Usage This solution works by using the AsyncTask#get() method. I'm making a workaround, though so I can emulate the behaviour. – Gonzalo Nov 04 '12 at 18:58