1. 主线程不能捕获到子线程的异常
- package Thread.Exection;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class ExeceptionThread implements Runnable {@Override public void run() {
- throw new RuntimeException();
- }
- public static void main(String[] args) {
- try {
- ExecutorService exec = Executors.newCachedThreadPool();
- exec.execute(new ExeceptionThread());
- } catch(Exception e) {
- System.out.println("exeception has handled");
- }
- }
- }
输出:
- Exception in thread "pool-1-thread-1" java.lang.RuntimeException at Thread.Exection.ExeceptionThread.run(ExeceptionThread.java:10) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
2. 通过设置 HandlerThreadFactory 捕获异常
- package Thread.Exection;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ThreadFactory;
- class ExceptionThread2 implements Runnable {
- public void run() {
- Thread t = Thread.currentThread();
- System.out.println("run() by " + t);
- System.out.println("eh=" + t.getUncaughtExceptionHandler());
- throw new RuntimeException();
- }
- }
- class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {@Override public void uncaughtException(Thread t, Throwable e) {
- System.out.println("caught " + e);
- }
- }
- class HandlerThreadFactory implements ThreadFactory {@Override public Thread newThread(Runnable r) {
- System.out.println(this + " creating new Thread");
- Thread t = new Thread(r);
- System.out.println("created " + t + " ID:" + t.getId());
- t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
- System.out.println("eh=" + t.getUncaughtExceptionHandler());
- return t;
- }
- }
- public class CaptureUncaughtException {
- public static void main(String[] args) {
- ExecutorService exec = Executors.newCachedThreadPool(new HandlerThreadFactory());
- exec.execute(new ExceptionThread2());
- }
- }
输出:
- Thread.Exection.HandlerThreadFactory@4e25154f creating new Threadcreated Thread[Thread-0,5,main] ID:9eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4erun() by Thread[Thread-0,5,main]eh=Thread.Exection.MyUncaughtExceptionHandler@70dea4eThread.Exection.HandlerThreadFactory@4e25154f creating new Threadcreated Thread[Thread-1,5,main] ID:11eh=Thread.Exection.MyUncaughtExceptionHandler@193fa958caught java.lang.RuntimeException
3. 通过设置默认异常捕获类捕获异常
- package Thread.Exection;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class SettingDefaultHandler {
- public static void main(String[] args) {
- Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
- ExecutorService exec = Executors.newCachedThreadPool();
- exec.execute(new ExceptionThread2());
- }
- }
输出:
- run() by Thread[pool-1-thread-1,5,main]eh=java.lang.ThreadGroup[name=main,maxpri=10]caught java.lang.RuntimeException
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: