8

我想在 doInBackground 中捕获线程异常并在 onPostExcecute 中打印错误消息。问题是我在 onPostExecute 中没有 Throwable 对象。如何在非 UI 线程中捕获异常并在 UI 线程打印错误消息

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) {
            // print the error of the Throwable "e".
            // The problem is I don't have the Throwable object here! So I can't check the type of exception.
        }

    }
}

Arun回答后更新:

这是我的 AsyncTask 包装类。它打算在 doInBackground 中处理异常,但我找不到一个好的解决方案。

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);
    }
}
4

3 回答 3

7

更改 to 的返回类型,doInBackground()Object您收到结果时,onPostExecute(Object result)使用instanceOf运算符检查返回的结果是 anException还是List<String>.

编辑

由于结果可以是异常或正确的列表,您可以使用以下内容:

protected void onPostExecute(final Object result) {
    working = false;
    if(result instanceof SomeCustomException) {
        exceptionHappendedListener.exceptionHappended(result);
    }
    else{
        if (onPostExecuteListener != null) {
            onPostExecuteListener.onPostExecute(result);
        }
    }
}

还要更改以下语句:

public abstract class AbstractWorkerTask<Params, Progress, Object> extends AsyncTask<Params, Progress, Object>
于 2012-06-08T07:18:32.643 回答
4

只需将异常存储到列表中并稍后处理,因为 onPostExecute() 总是在 doInBackground() 之后调用:

public class TestTask extends AsyncTask<Params, Progress, Result> {

  List<Exception> exceptions = new ArrayList<Exception>();

  @Override
  protected Result doInBackground(Params... params) {
    try {
      ...
    } catch(SomeCustomException e) {
      exceptions.add(e);
    }
    return result;
  }

  @Override
  protected void onPostExecute(Result result) {
    for (Exception e : exceptions) {
      // Do whatever you want for the exception here
      ...
    }
  }

}

这是可行的,但很少使用,因为在大多数情况下,我们希望在异常被抛出和捕获后立即处理:

public class TestTask extends AsyncTask<Params, Progress, Result> {

  @Override
  protected Result doInBackground(Params... params) {
    try {
      ...
    } catch(SomeCustomException e) {
      // If you need update UI, simply do this:
      runOnUiThread(new Runnable() {
        public void run() {
          // update your UI component here.
          myTextView.setText("Exception!!!");
        }
      });
    }
    return result;
  }

}

希望这是有道理的。

于 2012-06-08T11:04:39.337 回答
4

将 to的返回类型更改doInBackgroundObject可能传递一个Exception然后使用instanceof()是代码异味的来源(糟糕的编程习惯)。总是最好将您的返回类型限制为您想要返回的非常具体的东西。

基于这个答案,只需添加一个私有成员来存储抛出的异常doInBackground,然后首先在onPostExecute.

Exception需要捕获一个,因为doInBackground一旦抛出异常,您应该立即停止操作,并onPostExecute在您可以访问 UI 元素的地方优雅地处理它,以便将事故通知用户。

通用示例(AsyncTask 的主体):

private Exception mException

@Override
protected Result doInBackground(Params... params) {
    try {
          // --- Do something --- //
    }
    catch( SomeException e ){ mException = e; return null; }
}

@Override
protected void onPostExecute(Result result) {
    if (mException != null) {
        // --- handle exception --- //
        return;
    }

    // --- Perform normal post execution actions --- //
}
于 2013-11-04T20:36:41.820 回答