Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle AsyncTask failure

Tags:

java

android

Is there a specific way to handle failure in an AsyncTask? As far as I can tell the only way is with the return value of task. I'd like to be able to provide more details on the failure if possible, and null isn't very verbose.

Ideally it would provide an onError handler, but I don't think it has one.

class DownloadAsyncTask extends AsyncTask<String, Void, String> {

    /** this would be cool if it existed */
    @Override
    protected void onError(Exception ex) {
        ...
    }

    @Override
    protected String doInBackground(String... params) {
    try {
            ... download ...
        } catch (IOException e) {
            setError(e); // maybe like this?
        }
    }       
}
like image 871
leech Avatar asked Aug 20 '11 16:08

leech


People also ask

What happens to AsyncTask if activity is destroyed?

If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created. But the AsyncTask will not die. It will go on living until it completes.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.


2 Answers

You can simply save the exception in a field and check it in onPostExecute() (to ensure that any error handling code is run on the UI thread). Something like:

new AsyncTask<Void, Void, Boolean>() {
    Exception error;

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
             // do work
             return true;
        } catch (Exception e) {
            error = e;

            return false;
        } 
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            Toast.makeText(ctx, "Success!",
                Toast.LENGTH_SHORT).show();
         } else {
            if (error != null) {
                Toast.makeText(ctx, error.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}

like image 138
Nikolay Elenkov Avatar answered Sep 21 '22 10:09

Nikolay Elenkov


I modified Nicholas's code a bit, should you want to do something in the UI thread in exception.

Remember the AsyncTask can only be executed once after instantiated.

class ErrorHandlingAsyncTask extends AsyncTask<..., ..., ...> {

    private Exception exception = null;

    protected abstract void onResult(Result result);

    protected abstract void onException(Exception e);

    protected abstract ... realDoInBackground(...);

    @Override
    final protected void onPostExecute(Result result) {
        if(result != null) {
            onResult(result);
        } else {
            onException(exception);
        }
    }

    @Override
    protected ... doInBackground(...) {
        try {
            return realDoInBackground(...);
        } catch(Exception e) {
            exception = e;
        }
        return null;
    }
}
like image 24
dongshengcn Avatar answered Sep 24 '22 10:09

dongshengcn