1

During my implantation I need to throw several exceptions from a class, and handle them outside. Inside the class I made, I also implemented AsyncTask private class, and from this class , as well, I need to throw the exceptions. I realized that I cannot throw any exception from AsyncTask class, but only to handle it. This is not what I need.

Is there some kind of solution, so I'll be able to throw any exception I want from inside the AsyncTask?

YGT
  • 111
  • 1
  • 6

1 Answers1

2

I do something like below.
- Write your own implementation of MyListener class.Pass it in the constructor of MyAsyncTask class.
- Check the return value of doInBackground method,and call relevant method.

public interface MyListener {
    public void onSuccess();
    public abstract void onFail();
}

public class MyAsyncTask extends AsyncTask<String, Void, String>{  
    private MyListener listener;  
    public MyAsyncTask(MyListener listener){
        this.listener = listener;
}

    protected String doInBackground(String... params) {
        return aValue;
    }

    protected void onPostExecute(String aValue) {
        //Check aValue,if OK
        listener.onSuccess();
        //else
        listener.onFail();
    }
}
Winter
  • 1,004
  • 8
  • 13