AsyncTask and error handling on Android AsyncTask and error handling on Android android android

AsyncTask and error handling on Android


It works fine but is it the "right" approach and is there better alternative?

I hold onto the Throwable or Exception in the AsyncTask instance itself and then do something with it in onPostExecute(), so my error handling has the option of displaying a dialog on-screen.


Create an AsyncResult object ( which you can also use in other projects)

public class AsyncTaskResult<T> {    private T result;    private Exception error;    public T getResult() {        return result;    }    public Exception getError() {        return error;    }    public AsyncTaskResult(T result) {        super();        this.result = result;    }    public AsyncTaskResult(Exception error) {        super();        this.error = error;    }}

Return this object from your AsyncTask doInBackground methods and check it in the postExecute. ( You can use this class as a base class for your other async tasks )

Below is a mockup of a task that gets a JSON response from the web server.

AsyncTask<Object,String,AsyncTaskResult<JSONObject>> jsonLoader = new AsyncTask<Object, String, AsyncTaskResult<JSONObject>>() {        @Override        protected AsyncTaskResult<JSONObject> doInBackground(                Object... params) {            try {                // get your JSONObject from the server                return new AsyncTaskResult<JSONObject>(your json object);            } catch ( Exception anyError) {                return new AsyncTaskResult<JSONObject>(anyError);            }        }        protected void onPostExecute(AsyncTaskResult<JSONObject> result) {            if ( result.getError() != null ) {                // error handling here            }  else if ( isCancelled()) {                // cancel handling here            } else {                JSONObject realResult = result.getResult();                // result handling here            }        };    }


When I feel the need to handle Exceptions in AsyncTask properly, I use this as super class:

public abstract class ExceptionAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {    private Exception exception=null;    private Params[] params;    @Override    final protected Result doInBackground(Params... params) {        try {            this.params = params;             return doInBackground();        }        catch (Exception e) {            exception = e;            return null;        }    }    abstract protected Result doInBackground() throws Exception;    @Override    final protected void onPostExecute(Result result) {        super.onPostExecute(result);        onPostExecute(exception, result);    }    abstract protected void onPostExecute(Exception exception, Result result);    public Params[] getParams() {        return params;    }}

As normal, you override doInBackground in your subclass to do background work, happily throwing Exceptions where needed. You are then forced to implement onPostExecute (because it's abstract) and this gently reminds you to handle all types of Exception, which are passed as parameter. In most cases, Exceptions lead to some type of ui output, so onPostExecute is a perfect place to do that.