0
点赞
收藏
分享

微信扫一扫

Android Thread.UncaughtExce…


在Java 的异常处理机制中:
如果抛出的是Exception异常的话,必须有try..catch..进行处理,属于checked exception。
如果抛出的是RuntimeException异常的话,则不是必须进行try..catch..异常处理,发生异常之后将由JVM进行处理,属于unchecked exception。
注意:为了保证程序的健壮性,建议抛出RunntimeException异常,也使用try..catch..进行处理。

这两者最本质的区别在于设计者认为使用者是否能够并且应该处理这个异常。

Java 异常的分类:
基类为:Throwable
Error 和 Exception 继承于Throwable
RuntimeException和IOException等继承Exception
其中,Error和RuntimeException及其子类属于unchecked exception, 而其他异常为checked exception。


Error类描述了Java运行系统中的内部错误以及资源耗尽的情形,应用程序不应该抛出这种类型的对象(一般是由Java虚拟机抛出)。如果出现这种错误,除了尽力使程序安全退出外,在其他方面是无能为力的。所以,在我们在程序设计时,应该更关注Exception体系。

RuntimeExcption体系,包括错误的类型转换,数组越界访问和试图访问空指针等等。如果出现RuntimeException,那么一定是你自己的错误。

其他非RuntimeExcetpion(IOException等等),这类异常一般是外部错误,例如试图从文件尾后读取数据等,这并不是程序本身的错误,而是在应用环境中出现的外部错误。

在Android开发中,常常会出现uncheched Exception 导致程序的crash,为了提供良好的用户体验,并对出错的信息进行收集,以便对程序进行改进,提高程序的健壮性。因此,常使用Thread.UncaughtExceptionHandler 来进行处理。

首先需要继承Thread.UncaughtExceptionHandler 类


    1. public class CrashHandler implements Thread.UncaughtExceptionHandler {  
    2.    public static final String TAG = CrashHandler.class.getSimpleName();  
    3.    private static CrashHandler INSTANCE = new CrashHandler();  
    4.    private Context mContext;  
    5.    private Thread.UncaughtExceptionHandler mDefaultHandler;  
    6.  
    7.  
    8.    private CrashHandler() {  
    9.    }  
    10.  
    11.  
    12.    public static CrashHandler getInstance() {  
    13.        return INSTANCE;  
    14.    }  
    15.  
    16.  
    17.    public void init(Context ctx) {  
    18.        mContext = ctx;  
    19.        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();  
    20.        Thread.setDefaultUncaughtExceptionHandler(this);  
    21.    }  
    22.  
    23.  
    24.    @Override  
    25.    public void uncaughtException(Thread thread, Throwable ex) {  
    26.        // if (!handleException(ex) && mDefaultHandler != null) {   
    27.        // mDefaultHandler.uncaughtException(thread, ex);   
    28.        // } else {   
    29.        // android.os.Process.killProcess(android.os.Process.myPid());   
    30.        // System.exit(10);   
    31.        // }   
    32.        System.out.println("uncaughtException");  
    33.  
    34.  
    35.        new Thread() {  
    36.            @Override  
    37.            public void run() {  
    38.                Looper.prepare();  
    39.                new AlertDialog.Builder(mContext).setTitle("提示").setCancelable(false)  
    40.                        .setMessage("程序崩溃了...").setNeutralButton("我知道了", new OnClickListener() {  
    41.                            @Override  
    42.                            public void onClick(DialogInterface dialog, int which) {  
    43.                                System.exit(0);  
    44.                            }  
    45.                        })  
    46.                        .create().show();  
    47.                Looper.loop();  
    48.            }  
    49.        }.start();  
    50.    }  
    51.  
    52.  
    53.      
    54.    private boolean handleException(Throwable ex) {  
    55.        if (ex == null) {  
    56.            return true;  
    57.        }  
    58.        // new Handler(Looper.getMainLooper()).post(new Runnable() {   
    59.        // @Override   
    60.        // public void run() {   
    61.        // new AlertDialog.Builder(mContext).setTitle("提示")   
    62.        // .setMessage("程序崩溃了...").setNeutralButton("我知道了", null)   
    63.        // .create().show();   
    64.        // }   
    65.        // });   
    66.  
    67.  
    68.        return true;  
    69.    }  
    70. }



    然后在Application类中进行注册


    1. public class MyApplication extends Application{  
    2.  
    3.  
    4.    @Override  
    5.    public void onCreate(){  
    6.    super.onCreate();  
    7.        initErrorHandler();  
    8.    }  
    9.  
    10.  
    11.   private void initErrorHandler(){  
    12.    CrashHandler handler = CrashHandler.getInstance();  
    13.    handler.init(this);  
    14.   }  
    15. }

    举报

    相关推荐

    0 条评论