Skip to content

Commit 9afac2e

Browse files
UncaughtExceptionHandler
1 parent f571173 commit 9afac2e

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Java并发编程
99
线程六大生命周期
1010
Thread和Object中的重要方法
1111
线程的属性
12+
未捕获异常的处理
1213
ThreadPool
1314
ThreadLocal
1415

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package uncaughtexception;
2+
3+
/**
4+
* 说明线程的异常不能用传统方法捕获
5+
*
6+
* @Author: zzStar
7+
* @Date: 10-18-2020 16:15
8+
*/
9+
public class CantCatchDirectly implements Runnable {
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
13+
// 失效的原因 -> try语句里只能捕获对应线程内的异常,是针对主线程的,没有办法捕获到子线程中的异常
14+
try {
15+
new Thread(new CantCatchDirectly(), "th1").start();
16+
Thread.sleep(300);
17+
new Thread(new CantCatchDirectly(), "th2").start();
18+
Thread.sleep(300);
19+
new Thread(new CantCatchDirectly(), "th3").start();
20+
Thread.sleep(300);
21+
new Thread(new CantCatchDirectly(), "th4").start();
22+
Thread.sleep(300);
23+
} catch (RuntimeException e) {
24+
// 并未执行到
25+
System.out.println("Caught Exception");
26+
}
27+
28+
}
29+
30+
/*
31+
@Override
32+
public void run() {
33+
throw new RuntimeException();
34+
}
35+
*/
36+
37+
// 方案一,手动在每个run方法里进行try catch
38+
@Override
39+
public void run() {
40+
try {
41+
throw new RuntimeException();
42+
} catch (RuntimeException e) {
43+
System.out.println("Caught Exception");
44+
}
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package uncaughtexception;
2+
3+
/**
4+
* 单线程 -> 抛出,处理,有异常堆栈
5+
* 多线程 -> 子线程异常无法用传统方法捕获
6+
*
7+
* @Author: zzStar
8+
* @Date: 10-18-2020 16:11
9+
*/
10+
public class ExceptionInChildThread implements Runnable {
11+
12+
public static void main(String[] args) {
13+
new Thread(new ExceptionInChildThread()).start();
14+
// 主线程并未受到任何影响
15+
for (int i = 0; i < 100; i++) {
16+
System.out.println(i);
17+
}
18+
}
19+
20+
@Override
21+
public void run() {
22+
throw new RuntimeException();
23+
}
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package uncaughtexception;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
/**
7+
* 方案二 -> UncaughtExceptionHandler
8+
*
9+
* @Author: zzStar
10+
* @Date: 10-18-2020 16:33
11+
*/
12+
public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
13+
14+
private String name;
15+
16+
public MyUncaughtExceptionHandler(String name) {
17+
this.name = name;
18+
}
19+
20+
@Override
21+
public void uncaughtException(Thread t, Throwable e) {
22+
Logger logger = Logger.getAnonymousLogger();
23+
logger.log(Level.WARNING, "线程异常,终止" + t.getName(), e);
24+
System.out.println(name + "捕获了异常" + t.getName() + "异常" + e);
25+
}
26+
27+
28+
}
29+
30+
/*
31+
public void uncaughtException(Thread t, Throwable e) {
32+
if (parent != null) {
33+
// 递归,一直向上找
34+
parent.uncaughtException(t, e);
35+
} else {
36+
// 调用setDefaultUncaughtExceptionHandler方法设置的全局handler进行处理
37+
Thread.UncaughtExceptionHandler ueh =
38+
Thread.getDefaultUncaughtExceptionHandler();
39+
if (ueh != null) {
40+
ueh.uncaughtException(t, e);
41+
} else if (!(e instanceof ThreadDeath)) {
42+
// 全局handler不存在就输出异常栈
43+
System.err.print("Exception in thread \""
44+
+ t.getName() + "\" ");
45+
e.printStackTrace(System.err);
46+
}
47+
}
48+
}
49+
*/
224 KB
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package uncaughtexception;
2+
3+
/**
4+
* @Author: zzStar
5+
* @Date: 10-18-2020 16:45
6+
*/
7+
public class UserOwnUncaughtExceptionHandler implements Runnable {
8+
9+
public static void main(String[] args) throws InterruptedException {
10+
11+
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("MyHandler"));
12+
13+
new Thread(new UserOwnUncaughtExceptionHandler(), "th1").start();
14+
Thread.sleep(300);
15+
new Thread(new UserOwnUncaughtExceptionHandler(), "th2").start();
16+
Thread.sleep(300);
17+
new Thread(new UserOwnUncaughtExceptionHandler(), "th3").start();
18+
Thread.sleep(300);
19+
new Thread(new UserOwnUncaughtExceptionHandler(), "th4").start();
20+
Thread.sleep(300);
21+
22+
System.out.println("Caught Exception");
23+
24+
25+
}
26+
27+
28+
@Override
29+
public void run() {
30+
throw new RuntimeException();
31+
}
32+
}

0 commit comments

Comments
 (0)