1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Java捕获并处理线程失败抛出的异常

Java捕获并处理线程失败抛出的异常

时间:2023-03-26 06:53:01

相关推荐

Java捕获并处理线程失败抛出的异常

使用 UncaughtExceptionHandler

示例代码如下:

Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {public void uncaughtException(Thread th, Throwable ex) {System.out.println("Uncaught exception: " + ex);}};Thread thread = new Thread() {public void run() {System.out.println("Sleeping ...");try {Thread.sleep(1000);} catch (InterruptedException e) {System.out.println("Interrupted.");}System.out.println("Throwing exception ...");throw new RuntimeException();}};thread.setUncaughtExceptionHandler(handler);thread.start();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。