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 o
I combined momo's and Dongshengcn's answers, and created my own base class with both background and foreground exception handling (in case you want to do some serious error logging)
The thing is, my code encapsulates all of the ResultOrError class stuff and simply lets you return the normal result or throw an exception
public abstract class HandledAsyncTask extends
AsyncTask> {
/**
* Wraps the calling of the {@link #doTask(Object[])} method, also handling
* the exceptions possibly thrown.
*/
protected final ResultOrException doInBackground(Params... params) {
try {
Result res = doTask(params);
return new ResultOrException(res);
} catch (Exception e) {
onBackgroundException(e);
return new ResultOrException(e);
}
}
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to
* {@link #doTask(Object[])} by the caller of this task. This method can
* call {@link #publishProgress(Object...)} to publish updates on the UI
* thread.
*
* @param params
* The parameters of the task.
* @return A result, defined by the subclass of this task.
*/
protected abstract Result doTask(Params[] params);
/**
* Handles calling the {@link #onSuccess(Object)} and
* {@link #onFailure(Exception)} methods.
*/
@Override
protected final void onPostExecute(ResultOrException result) {
if (result.getException() != null) {
onFailure(result.getException());
} else {
onSuccess(result.getResult());
}
}
/**
* Called when an exception was thrown in {@link #doTask(Object[])}. Handled
* in the background thread.
*
* @param exception
* The thrown exception
*/
protected void onBackgroundException(Exception exception) {
}
/**
* Called when the {@link #doTask(Object[])} method finished executing with
* no exceptions thrown.
*
* @param result
* The result returned from {@link #doTask(Object[])}
*/
protected void onSuccess(Result result) {
}
/**
* Called when an exception was thrown in {@link #doTask(Object[])}. Handled
* in the foreground thread.
*
* @param exception
* The thrown exception
*/
protected void onFailure(Exception exception) {
}
}
class ResultOrException {
/**
* The possibly thrown exception
*/
Exception mException;
/**
* The result, if no exception was thrown
*/
TResult mResult;
/**
* @param exception
* The thrown exception
*/
public ResultOrException(Exception exception) {
mException = exception;
}
/**
* @param result
* The result returned from the method
*/
public ResultOrException(TResult result) {
mResult = result;
}
/**
* @return the exception
*/
public Exception getException() {
return mException;
}
/**
* @param exception
* the exception to set
*/
public void setException(Exception exception) {
mException = exception;
}
/**
* @return the result
*/
public TResult getResult() {
return mResult;
}
/**
* @param result
* the result to set
*/
public void setResult(TResult result) {
mResult = result;
}
}