的AsyncTask的catch异常。需要思考 [英] Catch Exception of AsyncTask. Need Thinking

查看:168
本文介绍了的AsyncTask的catch异常。需要思考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要赶异常doInBackground一个线程,并在onPostExcecute打印错误消息。问题是,我没有在onPostExecute的Throwable对象。如何在非UI线程的捕获异常在UI线程进行提示

 公共类TestTask扩展的AsyncTask<虚空,虚空,列表和LT;字符串>> {

    @覆盖
    受保护的名单,其中,字符串> doInBackground(最终空... PARAMS){
        尝试 {
            ...
            返回listOfString;
        }赶上(SomeCustomException E){
            ...
            返回null;
        }
    }

    @覆盖
    保护无效onPostExecute(最终名单,其中,字符串>的结果){
        如果(结果== NULL){
            //打印的ThrowableE的错误。
            //问题是我没有Throwable对象在这里!所以我无法检查异常的类型。
        }

    }
}
 

后,阿伦的回答更新:

这是我的AsyncTask的包装类。它打算做处理异常在doInBackground但我不能找到一个很好的解决办法做到这一点。

 公共抽象类AbstractWorkerTask< PARAMS,进展,结果>
延伸AsyncTask的< PARAMS,进展,结果>
实现可行{
    保护论preExecuteListener preExecuteListener;
    保护OnPostExecuteListener<结果> onPostExecuteListener;
    保护ExceptionHappenedListener exceptionHappendedListener;
    私人布尔工作;

    @覆盖
    在preExecute保护无效(){
        如果(在preExecuteListener!= NULL){
            在preExecuteListener.on preExecute();
        }
        工作= TRUE;
    }

    @覆盖
    保护无效onPostExecute(最终结果的结果){
        工作= FALSE;
        如果(/* .........*/ ) {
            exceptionHappendedListener.exceptionHappended(E);
        }
        如果(onPostExecuteListener!= NULL){
            onPostExecuteListener.onPostExecute(结果);
        }
    }

    @覆盖
    公共布尔isWorking(){
        返回工作;
    }

    公共无效西顿preExecuteListener(决赛上preExecuteListener preExecuteListener){
        this.on preExecuteListener =在preExecuteListener;
    }

    公共无效setOnPostExecuteListener(最终OnPostExecuteListener<结果> onPostExecuteListener){
        this.onPostExecuteListener = onPostExecuteListener;
    }

    公共无效setExceptionHappendedListener(最终ExceptionHappenedListener exceptionHappendedListener){
        this.exceptionHappendedListener = exceptionHappendedListener;
    }

    公共接口上preExecuteListener {
        无效的preExecute();
    }

    公共接口OnPostExecuteListener<结果> {
        无效onPostExecute(最终结果的结果);
    }

    公共接口ExceptionHappenedListener {
        无效exceptionHappended(例外五);
    }
}
 

解决方案

修改的返回类型doInBackground()对象,当你收到结果 onPostExecute(对象结果)使用 INSTANCEOF 运营商检查返回结果是异常名单,其中,字符串>

修改

由于其结果可以是一个例外,否则适当的列表,你可以使用以下命令:

 保护无效onPostExecute(最终目标的结果){
    工作= FALSE;
    如果(结果的instanceof SomeCustomException){
        exceptionHappendedListener.exceptionHappended(结果);
    }
    其他{
        如果(onPostExecuteListener!= NULL){
            onPostExecuteListener.onPostExecute(结果);
        }
    }
}
 

也改变了下面的语句:

 公共抽象类AbstractWorkerTask< PARAMS,进步,对象>扩展AsyncTask的< PARAMS,进步,对象>
 

I want to catch exception of a thread in doInBackground and print the error message in onPostExcecute. The problem is I don't have the Throwable object in onPostExecute. How to catch Exception in non-UI thread and print the error message in UI-thread?

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.
        }

    }
}

Update after Arun's answer:

This is my AsyncTask wrapper class. It intends to do handling Exception in doInBackground but I can't find a good solution to do it.

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

解决方案

Change the return type of doInBackground() to Object and when you receive the result in onPostExecute(Object result) use the instanceOf operator to check if the returned result is an Exception or the List<String>.

Edit

Since the result can either be an Exception or else the proper List, you can use the following:

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

Also change the following statement:

public abstract class AbstractWorkerTask<Params, Progress, Object> extends AsyncTask<Params, Progress, Object>

这篇关于的AsyncTask的catch异常。需要思考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆