Skip to content

Commit 9c0e672

Browse files
author
bysocket
committed
线程中不可控异常的处理
1 parent 2bd5241 commit 9c0e672

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.javacore.thread.uncaughtexp;
2+
3+
import java.lang.Thread.UncaughtExceptionHandler;
4+
5+
/**
6+
* 描述:处理运行时异常的类
7+
* Created by bysocket on 16/3/4.
8+
*/
9+
public class ExceptionHadler implements UncaughtExceptionHandler{
10+
@Override
11+
public void uncaughtException(Thread t, Throwable e) {
12+
System.out.printf("An exception has been captured\n");
13+
System.out.printf("Thread: %s\n",t.getId());
14+
System.out.printf("Exception: %s: %s\n",e.getClass().getName(),e.getMessage());
15+
System.out.printf("Stack Trace: \n");
16+
e.printStackTrace(System.out);
17+
System.out.printf("Thread status: %s\n",t.getState());
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.javacore.thread.uncaughtexp;
2+
3+
/**
4+
* 描述:抛出运行时异常的线程类
5+
* Created by bysocket on 16/3/4.
6+
*/
7+
public class UncaughtTask implements Runnable {
8+
@Override
9+
public void run() {
10+
int numero = Integer.parseInt("TTTT");
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.javacore.thread.uncaughtexp;
2+
3+
/**
4+
* 描述:实现异常类Test
5+
* Created by bysocket on 16/3/4.
6+
*/
7+
public class UncaughtTest {
8+
public static void main(String[] args) {
9+
UncaughtTask uncaughtTask = new UncaughtTask();
10+
Thread thread = new Thread(uncaughtTask);
11+
thread.setUncaughtExceptionHandler(new ExceptionHadler());
12+
thread.start();
13+
}
14+
}

0 commit comments

Comments
 (0)