对线程中未捕获的异常进行处理UncaughtExceptionHandler

举报
陈皮的JavaLib 发表于 2021/06/09 23:01:35 2021/06/09
【摘要】 通常程序中我们会对可能出现的异常进行捕获,例如 public static void main(String[] args) { System.out.println("##### begin"); int dividend = 10; int divisor = 0; try { int result = dividend / divisor; } catch (E...

通常程序中我们会对可能出现的异常进行捕获,例如

public static void main(String[] args) { System.out.println("##### begin"); int dividend = 10; int divisor = 0; try { int result = dividend / divisor; } catch (Exception e) { System.out.println(e); } System.out.println("##### end");
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出结果

##### begin
java.lang.ArithmeticException: / by zero
##### end

  
 
  • 1
  • 2
  • 3

如果程序中我们没有对可能出现的异常进行捕获,例如

public static void main(String[] args) { System.out.println("##### begin"); int dividend = 10; int divisor = 0; int result = dividend / divisor; System.out.println("##### end");

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

输出结果

##### begin
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.yzj.ehr.controller.InnerController.main(InnerController.java:153)

  
 
  • 1
  • 2
  • 3

Thread类有个方法setUncaughtExceptionHandler(UncaughtExceptionHandler eh),表示我们可以对一个线程自定义自己的未捕获异常处理器。
我们只需写一个类继承UncaughtExceptionHandler接口,重写void uncaughtException(Thread t, Throwable e)方法,然后传入到Thread对象中。

public static void main(String[] args) { Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("这是对未捕获的异常进行处理方法"); System.out.println("Thread name:" + t.getName() + ",ex:" + e); } }); System.out.println("##### begin"); int dividend = 10; int divisor = 0; int result = dividend / divisor; System.out.println("##### end");

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

输出结果

##### begin
这是对未捕获的异常进行处理方法
Thread name:main,ex:java.lang.ArithmeticException: / by zero

  
 
  • 1
  • 2
  • 3

对不同线程定义不同的未捕获异常处理器

public static void main(String[] args) { // 定义main线程的 Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("这是对main线程中未捕获的异常进行处理方法"); System.out.println("@ Thread name:" + t.getName() + ",ex:" + e); } }); System.out.println("##### begin"); new Thread(new Runnable() { @Override public void run() { // 定义myThread线程的 Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("这是对myThread线程中未捕获的异常进行处理方法"); System.out.println("# Thread name:" + t.getName() + ",ex:" + e); } }); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e1) { e1.printStackTrace(); } // 模拟异常 int[] array = new int[2]; int value = array[2]; } }, "myThread").start(); int dividend = 10; int divisor = 0; int result = dividend / divisor; System.out.println("##### end");

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

输出结果

##### begin
这是对main线程中未捕获的异常进行处理方法
@ Thread name:main,ex:java.lang.ArithmeticException: / by zero
这是对myThread线程中未捕获的异常进行处理方法
# Thread name:myThread,ex:java.lang.ArrayIndexOutOfBoundsException: 2

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

文章来源: javalib.blog.csdn.net,作者:陈皮的JavaLib,版权归原作者所有,如需转载,请联系作者。

原文链接:javalib.blog.csdn.net/article/details/106274671

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。