在 AsyncTask 中捕获未处理异常的设计模式

发布于 2024-11-29 09:24:37 字数 2420 浏览 1 评论 0原文

各位,

我通过 onCreate 顶部的这样的代码片段捕获未处理的 Android 异常:

    try {
        File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory);
        crashLogDirectory.mkdirs();

        Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler(
                this, crashLogDirectory.getCanonicalPath()));
    } catch (Exception e) {
        if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString());
    }

我想为我在 Android 应用程序中使用的大约两打 AsyncTasks 提供类似的东西,因此 doInBackground 中发生的未处理的异常是抓住&已记录。

问题是,因为 AsyncTask 采用任意类型初始值设定项,所以我不确定如何声明一个超类(我的所有 AsyncTask 都继承自该超类)来设置此未处理的异常处理程序。

任何人都可以推荐一个好的设计模式来处理 AsyncTask 的 doInBackground 方法中未处理的异常,该模式不涉及为每个新的 AsyncTask 定义复制和粘贴上面的代码吗?

谢谢!

更新

这是我在仔细查看来源AsyncTask

import java.io.File;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public abstract class LoggingAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

    protected void setupUnhandledExceptionLogging(Context context) {
        try {
            File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory);
            crashLogDirectory.mkdirs();

            Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler(
                    context, crashLogDirectory.getCanonicalPath()));
        } catch (Exception e) {
            if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString());
        }

    }
}

然后我定义我的任务如下:

private class MyTask extends LoggingAsyncTask<Void, Void, HashMap<String, Object>> {
    protected HashMap<String, Object> doInBackground(Void... args) {
        this.setupUnhandledExceptionLogging(MyActivity.this.mContext);
        // do work
        return myHashMap;
  }
}

显然,您的任务可以采用此模式所需的任何参数。您可以定义 RemoteUploadExceptionHandler 来执行必要的日志记录/上传。

Folks,

I catch unhandled Android exceptions via a code snippet like this, at the top of onCreate:

    try {
        File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory);
        crashLogDirectory.mkdirs();

        Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler(
                this, crashLogDirectory.getCanonicalPath()));
    } catch (Exception e) {
        if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString());
    }

I'd like to come up with something similar for about two dozen AsyncTasks I use in my android application, so unhandled exceptions that occur in doInBackground are caught & logged.

Problem is, because AsyncTask takes arbitrary type initializers, I am not sure how to declare a superclass, from which all my AsyncTasks inherit, that sets up this unhandled exception handler.

Can anyone recommend a good design pattern for handling unhandled exceptions in the doInBackground method of AsyncTask that does not involve copy-and-paste of code like that above for every new AsyncTask definition?

Thanks!

UPDATE

Here is the design pattern I used, after looking more closely at the source of AsyncTask

import java.io.File;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public abstract class LoggingAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

    protected void setupUnhandledExceptionLogging(Context context) {
        try {
            File crashLogDirectory = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + Constants.CrashLogDirectory);
            crashLogDirectory.mkdirs();

            Thread.setDefaultUncaughtExceptionHandler(new RemoteUploadExceptionHandler(
                    context, crashLogDirectory.getCanonicalPath()));
        } catch (Exception e) {
            if (MyActivity.WARN) Log.e(ScruffActivity.TAG, "Exception setting up exception handler! " + e.toString());
        }

    }
}

Then I define my tasks as follows:

private class MyTask extends LoggingAsyncTask<Void, Void, HashMap<String, Object>> {
    protected HashMap<String, Object> doInBackground(Void... args) {
        this.setupUnhandledExceptionLogging(MyActivity.this.mContext);
        // do work
        return myHashMap;
  }
}

Obviously your task can take whatever params necessary with this pattern. It's up to you to define RemoteUploadExceptionHandler to do the necessary logging/uploading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘味果▽酱 2024-12-06 09:24:37

我不会将其称为设计模式,而只是包装 doInBackground() 并根据需要初始化和/或捕获异常。

public abstract class AsyncTaskWrapper<Params, Progress, Result> extends
        AsyncTask<Params, Progress, Result> {

    protected Exception error;

    protected Result doInBackground(Params... params) {
        try {
            init();

            return doRealWork(params);
        } catch (Exception e) {
            error = e;

            Log.e("TAG", e.getMessage(), e);

            return null;
        }
    }

    protected abstract void init();

    protected abstract Result doRealWork(Params... params);
}

I wouldn't go as far as to call it a design pattern, but just wrap doInBackground() and initialize and/or catch exceptions as necessary.

public abstract class AsyncTaskWrapper<Params, Progress, Result> extends
        AsyncTask<Params, Progress, Result> {

    protected Exception error;

    protected Result doInBackground(Params... params) {
        try {
            init();

            return doRealWork(params);
        } catch (Exception e) {
            error = e;

            Log.e("TAG", e.getMessage(), e);

            return null;
        }
    }

    protected abstract void init();

    protected abstract Result doRealWork(Params... params);
}
︶葆Ⅱㄣ 2024-12-06 09:24:37

esilver 我捕获异常并返回一个 BoolString 对象,一个无抛出模式

    //a utility class to signal success or failure, return an error message, and return a useful String value
//see Try Out in C#
public final class BoolString {
 public final boolean success;
 public final String err;
 public final String value;

 public BoolString(boolean success, String err, String value){
     this.success= success;
     this.err= err;
     this.value= value;
 }
}

用法:

private class MyAsynch extends AsyncTask<String, Void, BoolString>{
    protected BoolString doInBackground(String...strings) { // <== DO NOT TOUCH THE UI VIEW HERE      
        return model.tryMyMethod(...); // <== return value BoolString result is sent to onPostExecute
    }        

protected void onPostExecute(BoolString result){
            progress.dismiss();
            if (result.success){
                ... continue with result.value
            }
            else {
                ..log result.err
            }
        }

// NO THROWS VERSION Helper method
public BoolString tryMyMethod(...) {
    try {
        String value= MyMethod(...);
        return new BoolString(true,"",value);
    }
    catch (Exception e){
        return new BoolString(false,e.getMessage(),"");
    }
}

esilver I trap for exceptions and return a BoolString object, a no throws pattern

    //a utility class to signal success or failure, return an error message, and return a useful String value
//see Try Out in C#
public final class BoolString {
 public final boolean success;
 public final String err;
 public final String value;

 public BoolString(boolean success, String err, String value){
     this.success= success;
     this.err= err;
     this.value= value;
 }
}

USAGE:

private class MyAsynch extends AsyncTask<String, Void, BoolString>{
    protected BoolString doInBackground(String...strings) { // <== DO NOT TOUCH THE UI VIEW HERE      
        return model.tryMyMethod(...); // <== return value BoolString result is sent to onPostExecute
    }        

protected void onPostExecute(BoolString result){
            progress.dismiss();
            if (result.success){
                ... continue with result.value
            }
            else {
                ..log result.err
            }
        }

// NO THROWS VERSION Helper method
public BoolString tryMyMethod(...) {
    try {
        String value= MyMethod(...);
        return new BoolString(true,"",value);
    }
    catch (Exception e){
        return new BoolString(false,e.getMessage(),"");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文