android - 捕获 AsyncTask 的异常。需要思考

标签 android exception-handling

我想在 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);
    }
}

最佳答案

更改doInBackground()的返回类型至 Object当您在 onPostExecute(Object result) 中收到结果时使用 instanceOf运算符检查返回的结果是否为 ExceptionList<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>

关于android - 捕获 AsyncTask 的异常。需要思考,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10944442/

相关文章:

android - 处理 Android 库的 ant 构建的正确方法。构建从 jar 文件中排除 R.class

android - Apache Math 上的私有(private)访问

android - 拔下 USB 后,USB 附件保持连接

asp.net-mvc - ASP.Net MVC异常记录与错误处理结合

winforms - 无法捕获 FormClosing 中抛出的异常

python - 异常情况下 "resume next"的 Pythonic 方式?

java - Razorpay 支付 id 在 onPaymentError Android 中返回 null

Android Studio 不更新移动设备上的应用程序

php - 使用服务层在我的分层应用程序中向哪里抛出异常?

silverlight - RIA 服务和故障契约(Contract)