java主线程捕获子线程中的异常

时间:2024-06-26 14:35:08

本文主要参考:《think in java》

好,下面上货。

正常情况下,如果不做特殊的处理,在主线程中是不能够捕获到子线程中的异常的。
例如下面的情况。

  1. package com.xueyou.demo.theadexceptiondemo;
  2. public class ThreadExceptionRunner implements Runnable{
  3. @Override
  4. public void run() {
  5. throw new RuntimeException("error !!!!");
  6. }
  7. }
使用线程执行上面的任务

  1. package com.xueyou.demo.theadexceptiondemo;
  2. import com.sun.glass.ui.TouchInputSupport;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ThreadFactory;
  6. public class ThreadExceptionDemo {
  7. public static void main(String[] args) {
  8. try {
  9. Thread thread = new Thread(new ThreadExceptionRunner());
  10. thread.start();
  11. } catch (Exception e) {
  12. System.out.println("========");
  13. e.printStackTrace();
  14. } finally {
  15. }
  16. System.out.println(123);
  17. }
  18. }
执行结果如下:

java主线程捕获子线程中的异常

如果想要在主线程中捕获子线程的异常,我们需要使用ExecutorService,同时做一些修改。
如下:


  1. package com.xueyou.demo.theadexceptiondemo;
  2. import com.sun.glass.ui.TouchInputSupport;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ThreadFactory;
  6. public class ThreadExceptionDemo {
  7. public static void main(String[] args) {
  8. try {
  9. Thread thread = new Thread(new ThreadExceptionRunner());
  10. thread.start();
  11. } catch (Exception e) {
  12. System.out.println("========");
  13. e.printStackTrace();
  14. } finally {
  15. }
  16. System.out.println(123);
  17. ExecutorService exec = Executors.newCachedThreadPool(new HandleThreadFactory());
  18. exec.execute(new ThreadExceptionRunner());
  19. exec.shutdown();
  20. }
  21. }
  22. class MyUncaughtExceptionHandle implements Thread.UncaughtExceptionHandler {
  23. @Override
  24. public void uncaughtException(Thread t, Throwable e) {
  25. System.out.println("caught " + e);
  26. }
  27. }
  28. class HandleThreadFactory implements ThreadFactory {
  29. @Override
  30. public Thread newThread(Runnable r) {
  31. System.out.println("create thread t");
  32. Thread t = new Thread(r);
  33. System.out.println("set uncaughtException for t");
  34. t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandle());
  35. return t;
  36. }
  37. }
这样就能够捕获到异常了,运行结果如下:

java主线程捕获子线程中的异常

上面的方式是设置每一个线程执行时候的异常处理。如果每一个线程的异常处理相同,我们可以用如下的方式进行处理,使用Thread的静态方法。
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());
整体代码如下:


  1. package com.xueyou.demo.theadexceptiondemo;
  2. import com.sun.glass.ui.TouchInputSupport;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. import java.util.concurrent.ThreadFactory;
  6. /**
  7. * Created by wuxueyou on 2018/6/24.
  8. */
  9. public class ThreadExceptionDemo {
  10. public static void main(String[] args) {
  11. try {
  12. Thread thread = new Thread(new ThreadExceptionRunner());
  13. thread.start();
  14. } catch (Exception e) {
  15. System.out.println("========");
  16. e.printStackTrace();
  17. } finally {
  18. }
  19. System.out.println(123);
  20. Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandle());
  21. // ExecutorService exec = Executors.newCachedThreadPool(new HandleThreadFactory());
  22. ExecutorService exec = Executors.newCachedThreadPool();
  23. exec.execute(new ThreadExceptionRunner());
  24. exec.shutdown();
  25. }
  26. }
运行结果:

java主线程捕获子线程中的异常