I want to catch a stream exception in doInBackground and print an error message in onPostExcecute. The problem is that I don't have a Throwable object in onPostExecute. How to catch an exception in a thread without a user interface and print an error message in the user interface ?
public class TestTask extends AsyncTask<Void, Void, List<String>> { @Override protected List<String> doInBackground(final Void... params) { try { ... return listOfString; } catch(SomeCustomException e) { ... return null; } } @Override protected void onPostExecute(final List<String> result) { if(result == null) {
Update after Arun's answer:
This is my AsyncTask wrapper class. He intends to do Exception handling in doInBackground, but I cannot find a suitable solution for this.
public abstract class AbstractWorkerTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> implements Workable { protected OnPreExecuteListener onPreExecuteListener; protected OnPostExecuteListener<Result> onPostExecuteListener; protected ExceptionHappenedListener exceptionHappendedListener; private boolean working; @Override protected void onPreExecute() { if (onPreExecuteListener != null) { onPreExecuteListener.onPreExecute(); } working = true; } @Override protected void onPostExecute(final Result result) { working = false; if( ) { exceptionHappendedListener.exceptionHappended(e); } if (onPostExecuteListener != null) { onPostExecuteListener.onPostExecute(result); } } @Override public boolean isWorking() { return working; } public void setOnPreExecuteListener(final OnPreExecuteListener onPreExecuteListener) { this.onPreExecuteListener = onPreExecuteListener; } public void setOnPostExecuteListener(final OnPostExecuteListener<Result> onPostExecuteListener) { this.onPostExecuteListener = onPostExecuteListener; } public void setExceptionHappendedListener(final ExceptionHappenedListener exceptionHappendedListener) { this.exceptionHappendedListener = exceptionHappendedListener; } public interface OnPreExecuteListener { void onPreExecute(); } public interface OnPostExecuteListener<Result> { void onPostExecute(final Result result); } public interface ExceptionHappenedListener { void exceptionHappended(Exception e); } }
android exception-handling
Emerald214
source share