Java異常處理UncaughtExceptionHandler使用實(shí)例代碼詳解
異常處理
線程未捕獲異常 UncaughtException 需要UncaughtZExceptionHandler 來進(jìn)行處理
那么為什么非要用UncaughtZExceptionHandler呢?
- 主線程可以輕松捕獲線程,子線程不可以
- 從下面代碼可知,即使子線程拋出異常,主線程絲毫不受影響
public class ChildException implements Runnable{
public static void main(String[] args) {
new Thread(new ChildException()).start();
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
@Override
public void run() {
throw new RuntimeException();
}
}
/*
* Exception in thread "Thread-0" java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.ChildException.run(ChildException.java:14)
at java.lang.Thread.run(Thread.java:748)
0
1
2
3
4
5
6
7
8
9
* */
- 從下面代碼可知,即使想用catch捕獲子線程異常,時(shí)沒有用的
- try catch 是針對主線程的,沒有辦法捕獲子線程的異常
public class CantCatch implements Runnable {
public static void main(String[] args) throws InterruptedException {
try {
new Thread(new CantCatch(), "thread0").start();
Thread.sleep(300);
new Thread(new CantCatch(), "thread1").start();
Thread.sleep(300);
new Thread(new CantCatch(), "thread2").start();
Thread.sleep(300);
new Thread(new CantCatch(), "thread3").start();
Thread.sleep(300);
} catch (RuntimeException e) {
System.out.println("catch");
}
}
@Override
public void run() {
throw new RuntimeException();
}
}
/*
* Exception in thread "thread0" java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "thread1" java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "thread2" java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "thread3" java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.CantCatch.run(CantCatch.java:22)
at java.lang.Thread.run(Thread.java:748)
Process finished with exit code 0
* */
在run方法中進(jìn)行try catch可以捕獲到異常,但是特別麻煩,因?yàn)樾枰謩?dòng)地在每個(gè)run方法中都進(jìn)行try catch
UncaughtExceptionHandler
自定義UncaughtExceptionHandler
public class MyUncaughtHandler implements Thread.UncaughtExceptionHandler{
private String name;
public MyUncaughtHandler(String name) {
this.name = name;
}
@Override
public void uncaughtException(Thread t, Throwable e) {
Logger logger = Logger.getAnonymousLogger();
logger.log(Level.WARNING, "線程異常" + t.getName(), e);
System.out.println(name + "捕獲" + t.getName()+ e);
}
}
使用自定義的類來捕獲異常
public class UseOwnExceptionHandler implements Runnable {
public static void main(String[] args) throws InterruptedException {
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtHandler("MyHandler"));
// try {
new Thread(new UseOwnExceptionHandler(), "thread0").start();
Thread.sleep(300);
new Thread(new UseOwnExceptionHandler(), "thread1").start();
Thread.sleep(300);
new Thread(new UseOwnExceptionHandler(), "thread2").start();
Thread.sleep(300);
new Thread(new UseOwnExceptionHandler(), "thread3").start();
Thread.sleep(300);
// } catch (RuntimeException e) {
// System.out.println("catch");
// }
}
@Override
public void run() {
// try {
throw new RuntimeException();
// } catch (RuntimeException e) {
// System.out.println("e");
// }
}
}
/*
* 一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 線程異常thread0
java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
at java.lang.Thread.run(Thread.java:748)
MyHandler捕獲thread0java.lang.RuntimeException
一月 29, 2023 11:22:01 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 線程異常thread1
java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
at java.lang.Thread.run(Thread.java:748)
MyHandler捕獲thread1java.lang.RuntimeException
一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 線程異常thread2
java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
at java.lang.Thread.run(Thread.java:748)
MyHandler捕獲thread2java.lang.RuntimeException
一月 29, 2023 11:22:02 上午 com.jx.JavaTest.Thread.Exception.MyUncaughtHandler uncaughtException
警告: 線程異常thread3
java.lang.RuntimeException
at com.jx.JavaTest.Thread.Exception.UseOwnExceptionHandler.run(UseOwnExceptionHandler.java:24)
at java.lang.Thread.run(Thread.java:748)
MyHandler捕獲thread3java.lang.RuntimeException
Process finished with exit code 0
* */
到此這篇關(guān)于Java異常處理UncaughtExceptionHandler使用實(shí)例代碼詳解的文章就介紹到這了,更多相關(guān)Java UncaughtExceptionHandler內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式
本文主要介紹了mybatis?resultMap之collection聚集兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了java 計(jì)算中位數(shù)的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Spring MVC訪問靜態(tài)文件_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Spring MVC訪問靜態(tài)文件的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
java如何實(shí)現(xiàn)圖片轉(zhuǎn)化為數(shù)據(jù)流
這篇文章主要介紹了java如何實(shí)現(xiàn)圖片轉(zhuǎn)化為數(shù)據(jù)流,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Java?Socket編程從零到實(shí)戰(zhàn)詳解(完整實(shí)戰(zhàn)案例)
這篇文章主要介紹了Java?Socket編程從零到實(shí)戰(zhàn)詳解,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04

