关于多线程未捕获异常的处理介绍

 XML Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.lyzx.restdy.callable;

import java.util.concurrent.ThreadFactory;

public class T4 {
    public static void main(String[] args) {
        ex();
    }
    
    public static void ex(){
        try {
            new Thread(new ExceptionRunnable()).start();
        }catch(Exception e) {
            System.out.println( "捕获异常......");
        }
    }
}


class ExceptionRunnable implements Runnable{
    @Override
    public void run() {
        throw new RuntimeException( "抛异常了....");
    }
}
运行如下代码时发现手动抛出的异常并未被捕获,这就是java中多线程run方法中抛出的异常外部的catch是捕获不到的
运行结果如下
 XML Code 
1
2
3
Exception in thread  "Thread-0" java.lang.RuntimeException: 抛异常了....
    at com.lyzx.restdy.callable.ExceptionRunnable.run(T4.java:23)
    at java.lang.Thread.run(Unknown Source)

java为捕获异常的处理
 XML Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.lyzx.restdy.callable;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class T4 {
    public static void main(String[] args) {
         //ex();
        catchEx();
        
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    public static void ex(){
        try {
            new Thread(new ExceptionRunnable()).start();
        }catch(Exception e) {
            System.out.println( "捕获异常......");
        }
    }
    
    public static void catchEx(){
        MyFactory f = new MyFactory();
        ExceptionRunnable r = new ExceptionRunnable();
        Thread t = f.newThread(r);
        System.out.println( "catchEx:(任务)>>"+r);
        System.out.println( "catchEx:(线程)>>"+t);
        t.start();
    }
}

/**
 * 在线程中抛出异常
 * /
class ExceptionRunnable implements Runnable{
    @Override
    public void run() {
        RuntimeException e = new RuntimeException( "抛异常了....");
        System.out.println( "run:(异常)>>"+e);
        throw e;
    }
}

/**
 * 线程工厂
 * 为每一个从该工厂中生产的线程分配一个异常处理器
 * /
class MyFactory implements ThreadFactory{

    @Override
    public Thread newThread(Runnable r){
        Thread t = new Thread(r);
        t.setUncaughtExceptionHandler(new MyExceptionHandler());
        return t;
    }
}

/**
 * 异常处理器类
 * /
class MyExceptionHandler implements Thread.UncaughtExceptionHandler{

     /**
     * Thread t     抛异常的线程
     * Throwable e  t这个线程抛出的 异常
     * /
    @Override
    public void uncaughtException(Thread t,Throwable e) {
        System.out.println(t.getName()+ "被捕获,现在要做一些异常处理");
        System.out.println( "uncaughtException:(异常)>>"+e);
        System.out.println( "uncaughtException:(线程)>>"+t);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值