如果我们调用一个线程处理任务,但是当线程发生异常的时候我们调用线程的地方无法捕捉到异常。
用例子验证:我在main中把调用线程的代码放到try-catch里面,但是结果却没有捕捉到任何异常。
package thread;
class MRunnable implements Runnable{
@Override
public void run() {
throw new RuntimeException("This is an exception from Runnable");
}
}
public class TestException {
public static void main(String[] args) {
try{
new Thread(new MRunnable2()).start();
}catch (Exception e) {
System.out.println("catch: "+e.getMessage());
}
}
}
执行结果:
Exception in thread "Thread-0" java.lang.RuntimeException: This is an exception from Runnable
at thread.MRunnable2.run(TestException.java:6)
at java.lang.Thread.run(Unknown Source)
从结果看,console
并没有打印出“catch:”,证明没有捕捉到。
怎么才能在线程外捕捉到异常呢?
在Thread
类中有个UncaughtExceptionHandler
接口,如果有异常,JVM会调用dispatchUncaughtException()方法。
/**
* Dispatch an uncaught exception to the handler. This method is
* intended to be called only by the JVM.
*/
private void dispatchUncaughtException(Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}
因此我们只要一个实现了UncaughtExceptionHandler接口的类就可以了。
package thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
class MRunnable implements Runnable {
@Override
public void run() {
throw new RuntimeException("This is an exception from Runnable");
}
}
class MThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setUncaughtExceptionHandler(new MUncaughtExceptionHandler());
return thread;
}
}
class MUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("catch: "+e.getMessage());
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
}
public class TestException {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool(new MThreadFactory());
executorService.execute(new MRunnable());
}
}
执行结果:
>>>>>>>>>>>>>>>>>>>>>>>>>>>
catch: This is an exception from Runnable
<<<<<<<<<<<<<<<<<<<<<<<<<<<