Asynctask Error Handling

11,389

Solution 1

I think your code would to the job but there is already some kind of error handling built into the AsyncTask class.

You can avoid to use an extra variable by using the cancel() method and its handler method onCancelled(). When you call cancel within the doInBackground() method the onCancelled method in the UI thread. Whether you call cancel(true) or cancel(false) depends on your needs.


private class MyTask extends AsyncTask<String, Void, String>
{    
    @Override
    protected NewsItem doInBackground(String... params)
    {
        try
        {
            URL url = new URL("http://www.example.com/");
        }
        catch (MalformedURLException e)
        {
            cancel(false/true);  
        }

        // Other code here...

        return null;
    }

    @Override
    protected void onPostExecute(String result)
    {              
        // Perform successful post processing here...
    }

   @Override
    protected void onCancelled() {
        super.onCancelled();
        // Perform error post processing here...
    }
}

Solution 2

I've been doing that in my apps, I guess there is not a better way.

You can also read Mark Murphy answer about it.

Solution 3

This is guaranteed to work, even on SMP architecture. All the synchronization is done for you. It would however be better to use the return value to do this.

Share:
11,389
Leo
Author by

Leo

I am a student at Imperial College London and a part time Android developer.

Updated on June 04, 2022

Comments

  • Leo
    Leo almost 2 years

    I am using AsyncTask to perform some background calculations but I am unable to find a correct way to handle exceptions. Currently I am using the following code:

    private class MyTask extends AsyncTask<String, Void, String>
    {
        private int e = 0;
    
        @Override
        protected String doInBackground(String... params)
        {
            try
            {
                URL url = new URL("http://www.example.com/");
            }
            catch (MalformedURLException e)
            {
                e = 1;
            }
    
            // Other code here...
    
            return null;
        }
    
        @Override
        protected void onPostExecute(String result)
        {
            if (e == 1)
                Log.i("Some Tag", "An error occurred.");
    
            // Perform post processing here...
        }
    }
    

    I believe that the variable e maye be written/accessed by both the main and worker thread. As I know that onPostExecute() will only be run after doInBackround() has finished, can I omit any synchronization?

    Is this bad code? Is there an agreed or correct way to handle exceptions in an AsyncTask?

  • Leo
    Leo over 13 years
    If it causes no problems then I am happy!
  • Leo
    Leo over 13 years
    In the actual code I am using a custom object as the return type which doesn't really lend itself to holding exception information. I assume I can't overload the onPostExecute() function very easily?
  • Romain Guy
    Romain Guy over 13 years
    First of all, doInBackground must return an object of the same type as the parameter in onPostExecute(). Then you can just create a custom class to encapsulate the result + exception: class ResultHolder { Exception e; MyResult r; }
  • Leo
    Leo over 13 years
    This method only works if we have one type of exception. In my full code there are multiple things which can go wrong and they must be handled differently.
  • OneWorld
    OneWorld over 13 years
    Why? You can add to the try-clause as much catch-class as u want. In each catch-statement u can call the cancel-Method. You also can catch(Exception e) which catches all types of Exceptions. Did I get u right, Leo? However, is it good practice to call cancel() when Exceptions are thrown or is there an other way of Exception handling implemented?
  • DukeMe
    DukeMe about 9 years
    The cancel() way of handling the error is very close to what actually happens. Your processing has been canceled due to an error. You could still save some state on the ASyncTask that can be read by the UI thread to gain more information on what the error was.