Java 线程异常处理

如果我们调用一个线程处理任务,但是当线程发生异常的时候我们调用线程的地方无法捕捉到异常。
用例子验证:我在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
<<<<<<<<<<<<<<<<<<<<<<<<<<<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值