上代码
package com.cwj.test48;
import java.lang.Thread.UncaughtExceptionHandler;
public class UncaughtExceptionTest {
public static void main(String[] args) {
TestThread testThread = new TestThread();
testThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
// TODO Auto-generated method stub
System.out.println(t.getId() + "捕获到异常" + e.getClass().getName() + "\n" + e.getMessage());
e.printStackTrace(System.out);
}
});
testThread.start();
}
static class TestThread extends Thread {
@Override
public void run() {
super.run();
int c = Integer.parseInt("ff");
}
}
}
10捕获到异常java.lang.NumberFormatException
For input string: "ff"
java.lang.NumberFormatException: For input string: "ff"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.cwj.test48.UncaughtExceptionTest$TestThread.run(UncaughtExceptionTest.java:25)