实现 UncaughtExceptionHandler 类,重写 uncaughtException 方法。

public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(Thread.currentThread().getName() + "异常");
e.printStackTrace();
}
}

创建线程,并设置异常捕获

public static void main(String[] args) {
Thread thread = new Thread(() -> {
int b = 5 / 0;
}); // 设置所有线程
// Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler()); // 捕获指定线程中的异常
thread.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
thread.start();
}

最新文章

  1. SQL多表连接查询
  2. Java框架Struts2
  3. js选项卡
  4. K米--案例分析
  5. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
  6. SharePoint 服务器端对象模型 之 使用LINQ进行数据访问操作(Part 2)
  7. hp_jetdirect 9100漏洞检测
  8. Xamarin.iOS提供没有匹配的配置文件
  9. 关闭“JDK自动更新”提示
  10. Catalyst揭秘 Day3 sqlParser解析
  11. SCOI2010 and SXOI2014 股票交易(DP)
  12. LeetCode198 House Robber
  13. C语言的本质(34)——静态库
  14. [转]Intercepting the App Store's Traffic on iOS
  15. Java List&Map简单初始化方法
  16. Android OpenGL ES(八)绘制点Point ..
  17. zzuli 1815: easy problem 打表
  18. smtplib 报错501
  19. ASP.Net Core Razor+AdminLTE 小试牛刀
  20. MVC实例应用模式

热门文章

  1. Editplus code
  2. docker容器里面安装php的redis扩展
  3. 利用 Monitor.TryEnter 来规避 .NET 线程死锁的源代码
  4. IDEA springboot maven 项目部署
  5. Git学习笔记(2)-Eclipse中Git插件使用
  6. Scal(三)——类与对象
  7. 内嵌tomcat快速入门
  8. 神奇的系统bug
  9. 第二天Beta冲刺
  10. Eclipse指定jdk启动