forked from brianway/java-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun4_threadCreateException2.java
More file actions
45 lines (38 loc) · 1.38 KB
/
Copy pathRun4_threadCreateException2.java
File metadata and controls
45 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.brianway.learning.java.multithread.supplement.example4;
/**
* Created by Brian on 2016/4/17.
*/
/**
* P298
* 线程中出现异常,捕捉
*/
public class Run4_threadCreateException2 {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
t1.setName("thread t1");
t1.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("线程:" + t.getName() + " 出现了异常:");
e.printStackTrace();
}
});
t1.start();
Thread1 t2 = new Thread1();
t2.setName("thread t2");
t2.start();
}
}
/*
输出:
线程:thread t1 出现了异常:
Exception in thread "thread t2" java.lang.NullPointerException
at com.brianway.learning.java.multithread.supplement.example4.Thread1.run(Thread1.java:10)
java.lang.NullPointerException
at com.brianway.learning.java.multithread.supplement.example4.Thread1.run(Thread1.java:10)
------------------------------
Exception in thread "thread t2" 线程:thread t1 出现了异常:
java.lang.NullPointerException
at com.brianway.learning.java.multithread.supplement.example4.Thread1.run(Thread1.java:10)
java.lang.NullPointerException
at com.brianway.learning.java.multithread.supplement.example4.Thread1.run(Thread1.java:10)
*/