捕获子线程异常的解决方案
    1、手动在子线程的run方法里面try catch
    2、利用UncaughtExceptionhandler

    1. public void uncaughtException(Thread t, Throwable e) {
    2. if (parent != null) {
    3. parent.uncaughtException(t, e);
    4. } else {
    5. Thread.UncaughtExceptionHandler ueh =
    6. Thread.getDefaultUncaughtExceptionHandler();
    7. if (ueh != null) {
    8. ueh.uncaughtException(t, e);
    9. } else if (!(e instanceof ThreadDeath)) {
    10. System.err.print("Exception in thread \""
    11. + t.getName() + "\" ");
    12. e.printStackTrace(System.err);
    13. }
    14. }
    1. package com.imooc.thread_demo.uncaughtexception;
    2. /**
    3. * @Author: zhangjx
    4. * @Date: 2020/9/12 23:15
    5. * @Description:
    6. */
    7. public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{
    8. private String name;
    9. public MyUncaughtExceptionHandler(String name) {
    10. this.name = name;
    11. }
    12. @Override
    13. public void uncaughtException(Thread t, Throwable e) {
    14. System.out.println(name + "捕获" + t.getName() + " 线程异常,中止!");
    15. }
    16. }
    1. package com.imooc.thread_demo.uncaughtexception;
    2. /**
    3. * @Author: zhangjx
    4. * @Date: 2020/9/12 23:19
    5. * @Description:
    6. */
    7. public class UseMyUncaughtExceptionHandler {
    8. public static void main(String[] args) {
    9. Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler("自定义捕获器1"));
    10. new Thread(new SubThread()).start();
    11. }
    12. static class SubThread implements Runnable{
    13. @Override
    14. public void run() {
    15. throw new RuntimeException("子线程运行异常!");
    16. }
    17. }
    18. }