Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

目录

异常捕获

setUncaughtExceptionHandler

public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

设置该线程由于未捕获到异常而突然终止时调用的处理程序。

通过明确设置未捕获到的异常处理程序,线程可以完全控制它对未捕获到的异常作出响应的方式。

如果没有设置这样的处理程序,则该线程的 ThreadGroup 对象将充当其处理程序。

public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()

返回该线程由于未捕获到异常而突然终止时调用的处理程序。

如果该线程尚未明确设置未捕获到的异常处理程序,则返回该线程的 ThreadGroup 对象,除非该线程已经终止,在这种情况下,将返回 null。

setDefaultUncaughtExceptionHandler

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)

设置当线程由于未捕获到异常而突然终止,并且没有为该线程定义其他处理程序时所调用的默认处理程序。

未捕获到的异常处理首先由线程控制,然后由线程的 ThreadGroup 对象控制,最后由未捕获到的默认异常处理程序控制。

如果线程不设置明确的未捕获到的异常处理程序,并且该线程的线程组(包括父线程组)未特别指定其 uncaughtException 方法,则将调用默认处理程序的 uncaughtException 方法。

通过设置未捕获到的默认异常处理程序,应用程序可以为那些已经接受系统提供的任何“默认”行为的线程改变未捕获到的异常处理方式(如记录到某一特定设备或文件)。

请注意,未捕获到的默认异常处理程序通常不应顺从该线程的 ThreadGroup 对象,因为这可能导致无限递归。

public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()

返回线程由于未捕获到异常而突然终止时调用的默认处理程序。如果返回值为 null,则没有默认处理程序。

Thread.UncaughtExceptionHandler

public static interface Thread.UncaughtExceptionHandler

所有已知实现类:ThreadGroup

当 Thread 因未捕获的异常而突然终止时,调用处理程序的接口。

当某一线程因未捕获的异常而即将终止时,Java 虚拟机将使用 Thread.getUncaughtExceptionHandler() 查询该线程以获得其 UncaughtExceptionHandler 的线程,并调用处理程序的 uncaughtException 方法,将线程和异常作为参数传递。

如果某一线程没有明确设置其 UncaughtExceptionHandler,则将它的 ThreadGroup 对象作为其 UncaughtExceptionHandler。如果 ThreadGroup 对象对处理异常没有什么特殊要求,那么它可以将调用转发给默认的未捕获异常处理程序。

void uncaughtException(Thread t, Throwable e) 

当给定线程因给定的未捕获异常而终止时,调用该方法。

Java 虚拟机将忽略该方法抛出的任何异常。

案例

JAVA 测试案例

public class Test {
public static void main(String[] args) {
setDefaultUncaughtExceptionHandler();
test();
} private static void test() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("子线程异常前");
System.out.println(1 / 0);
}
}).start();
System.out.println("当前线程异常前");
System.out.println(1 / 0);
System.out.println("异常后的代码不能执行了");
} private static void setDefaultUncaughtExceptionHandler() {
UncaughtExceptionHandler currentHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("【当前线程的Handler处理异常信息】" + t.toString() + "\n" + e.getMessage());
}
};
UncaughtExceptionHandler defaultHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
printWriter.write("start------------\n");
e.printStackTrace(printWriter);
printWriter.write("------------end");
printWriter.close();
System.out.println("【默认的Handler处理异常信息】" + writer.getBuffer().toString());
}
};
Thread.currentThread().setUncaughtExceptionHandler(currentHandler);
Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
}
}

运行结果

子线程异常前
当前线程异常前
【当前线程的Handler处理异常信息】Thread[main,5,main]
/ by zero
【默认的Handler处理异常信息】start------------
java.lang.ArithmeticException: / by zero
at Test$1.run(Test.java:16)
at java.lang.Thread.run(Thread.java:745)
------------end

Android 中的一个实用案例

异常处理类

/**
* Desc:采集崩溃日志
*
* @author <a href="http://www.cnblogs.com/baiqiantao">白乾涛</a><p>
* @tag 崩溃日志<p>
* @date 2018/5/2 14:12 <p>
*/
public class CrashHandler implements Thread.UncaughtExceptionHandler {
private static final String LOG_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/crashLog/";
private Application application; private static CrashHandler instance = new CrashHandler(); private CrashHandler() {//构造方法私有
} public static CrashHandler getInstance() {
return instance;
} public void init(Application application) {
this.application = application;
Thread.setDefaultUncaughtExceptionHandler(instance);//设置该CrashHandler为系统默认的
} @Override
public void uncaughtException(Thread thread, Throwable ex) {
saveInfoToFile(collectCrashInfo(ex));//保存错误信息
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(application, "程序开小差了,将会在2秒后退出", Toast.LENGTH_SHORT).show();//使用Toast来显示异常信息
Looper.loop();
}
}.start();
SystemClock.sleep(2000);//延迟2秒杀进程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
} private String collectCrashInfo(Throwable ex) {
if (ex == null) return ""; Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
ex.printStackTrace(printWriter);
Throwable throwable = ex.getCause();
while (throwable != null) {
throwable.printStackTrace(printWriter);
throwable = throwable.getCause();//逐级获取错误信息
}
String crashInfo = writer.toString();
Log.i("bqt", "【错误信息】" + crashInfo);
printWriter.close();
return crashInfo;
} private void saveInfoToFile(String crashInfo) {
try {
File dir = new File(LOG_PATH);
if (!dir.exists()) {
dir.mkdirs();
}
String date = new SimpleDateFormat("yyyy.MM.dd_HH_mm_ss", Locale.getDefault()).format(new Date());
String fileName = LOG_PATH + "crash_" + date + ".txt";
FileWriter writer = new FileWriter(fileName);//如果保存失败,很可能是没有写SD卡权限
writer.write(crashInfo);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

获取的异常信息

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at com.bqt.test.MainActivity.onListItemClick(MainActivity.java:34)
at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1705)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:4171)
at android.widget.AbsListView$13.run(AbsListView.java:6735)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424)

2018-6-1

异常捕获 UncaughtExceptionHandler MD的更多相关文章

  1. 异常 Exception 堆栈跟踪 异常捕获 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. 异常捕获 崩溃 Bugly ACRC 简介 总结 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. iphone 异常捕获处理

    iphone 异常捕获处理 1 void UncaughtExceptionHandler(NSException *exception) { 2 NSArray *arr = [exception ...

  4. iOS异常捕获

    文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...

  5. JAVA并发,线程异常捕获

    由于线程的特性,当我们启动了线程是没有办法用try catch捕获异常的,如下例: package com.xt.thinks21_2; import java.util.concurrent.Exe ...

  6. android 运行时异常捕获

    1,将运行时异常捕获并存到手机SD卡上 可以直接使用logcat 命令Runtime.getRuntime().exec("logcat -f "+ file.getAbsolut ...

  7. Android全局异常捕获

    PS:本文摘抄自<Android高级进阶>,仅供学习使用 Java API提供了一个全局异常捕获处理器,Android引用在Java层捕获Crash依赖的就是Thread.Uncaught ...

  8. Android进阶——Crash异常捕获并发送到服务器

    在项目中,我们常常会遇到Crash的现象,也就是程序崩溃的时候,这个时候最常看到的就是这个界面 如果你的项目已经发布到市场上了,这样的崩溃对于开发人员是看不到的,所以我们得想方法将崩溃信息发送到服务器 ...

  9. .NET 基础 一步步 一幕幕[数组、集合、异常捕获]

    数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类 ...

随机推荐

  1. BZOJ2090 : [Poi2010]Monotonicity 2

    设f[i]表示以i为结尾的最长的合法序列的长度,=号直接维护,<号和>号用两棵树状数组维护即可,时间复杂度$O(n\log n)$. #include<cstdio> #def ...

  2. JDK版本的特性

    1. 2.JDK1.5增加的是: (1) 泛型与枚举类型(枚举类型是静态,常量.里面也可以包含构造方法,成员方法,但是构造方法一定是私有的. 适合枚举的是 有限数据,封装特定的数据,), (2)注解( ...

  3. hdu 5761 Rower Bo 物理题

    Rower Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 Description There is a river on the Ca ...

  4. 打印函数 lodop

    Lodop属性和方法详解 例子:LODOP.PRINT_INIT("打印任务名");LODOP.SET_PRINT_COPIES(2);bdhtml=window.document ...

  5. BootstrapClassloader ExtClassloader AppClassloader

    http://www.importnew.com/26269.html   import java.net.URL; class test9 { public static void main(Str ...

  6. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  7. javascript: break跳出多重循环以及退出each循环

    先来看一个小例子: <html> <body> <script type="text/javascript"> for(j=0;j<2;j ...

  8. Spring主从数据库的配置和动态数据源切换原理

    原文:https://www.liaoxuefeng.com/article/00151054582348974482c20f7d8431ead5bc32b30354705000 在大型应用程序中,配 ...

  9. Linux init 0-6 启动级别

    原文地址:http://blog.sina.com.cn/s/blog_5f8e8d9801010wlr.html 原文地址:[转]Linux init 0-6 启动级别作者:流水清风 init 0- ...

  10. YII框架分析笔记2:组件和事件行为管理

    Yii是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.CComponent几乎是所有类的基类,它控制着组件与事件的管理,其方法与属性如下,私有变量$_e数据存放事件(evnet,有些 ...