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