Android捕获未知异常的方法

捕获未使用try catch捕获的异常可以使用java接口UncaughtExceptionHandler来实现默认的捕获异常的机制,当程序执行时发生未知异常时,可以通过实现UncaughtExceptionHandler来捕获。

用法:

1.先实现UncaughtExceptionHandler接口

实现UncaughtExceptionHandler接口后重写uncaughtException方法,当有未知异常发生时,就会执行到此方法。

需要注意的是,需要调用父接口UncaughtExceptionHandler的uncaughtException方法,否则会发生ANR,导致无法弹出【很抱歉,"XXX"已停止运行。】的对话框。

public class CustomExceptionHandler implements UncaughtExceptionHandler {
	private static final String TAG = "CustomExceptionHandler";
	
	private UncaughtExceptionHandler mDefaultUEH;
	
	public CustomExceptionHandler() {
		Log.d(TAG, "------------ CustomExceptionHandler ------------");
		mDefaultUEH = Thread.getDefaultUncaughtExceptionHandler();
	}
	
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		Log.e(TAG, "------------ uncaughtException ------------ " + ex.getMessage());
		
		mDefaultUEH.uncaughtException(thread, ex); // 不加本语句会导致ANR
	}

}

2.在Activity类中设置CustomExceptionHandler类为默认的异常捕获类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler());
        
        int a = 1 / 0;
    }
当执行到1/0发生除数为0的异常时,就会被CustomExceptionHandler类的uncaughtException方法捕获,即执行到此方法,用户可以做相应的处理,例如释放资源,保存设置等。

3.运行程序,log输出如下:

08-14 15:21:45.265: D/CustomExceptionHandler(13482): ------------ CustomExceptionHandler ------------
08-14 15:21:45.265: D/AndroidRuntime(13482): Shutting down VM
08-14 15:21:45.265: W/dalvikvm(13482): threadid=1: thread exiting with uncaught exception (group=0x40fbc300)
08-14 15:21:45.265: E/CustomExceptionHandler(13482): ------------ uncaughtException ------------ Unable to start activity ComponentInfo{com.test.andtest/com.test.andtest.MainActivity}: java.lang.ArithmeticException: divide by zero
08-14 15:21:45.265: E/AndroidRuntime(13482): FATAL EXCEPTION: main
08-14 15:21:45.265: E/AndroidRuntime(13482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.andtest/com.test.andtest.MainActivity}: java.lang.ArithmeticException: divide by zero
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.ActivityThread.access$700(ActivityThread.java:131)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.os.Looper.loop(Looper.java:137)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.ActivityThread.main(ActivityThread.java:4866)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at java.lang.reflect.Method.invoke(Method.java:511)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at dalvik.system.NativeStart.main(Native Method)
08-14 15:21:45.265: E/AndroidRuntime(13482): Caused by: java.lang.ArithmeticException: divide by zero
08-14 15:21:45.265: E/AndroidRuntime(13482):     at com.test.andtest.MainActivity.onCreate(MainActivity.java:15)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.Activity.performCreate(Activity.java:5143)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-14 15:21:45.265: E/AndroidRuntime(13482):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-14 15:21:45.265: E/AndroidRuntime(13482):     ... 11 more




发布了270 篇原创文章 · 获赞 274 · 访问量 377万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览