(一)线程管理_7---处理线程不受控制的异常

处理线程不受控制的异常

在Java中有两种异常:java

  1. 受检查异常(Checked exceptions):这些异常必须显示的指定throws子句,或者使用try..catch语句;如IOException,ClassNotFoundException;
  2. 运行时异常或者叫作未受检查异常(unchecked exception or runtime exception ):这类异常不须要显示的指定throws或try..catch语句;如NumberFormatException;

当受检查异常在线程的run方法中被抛出,咱们必须捕获它并处理,由于run方法签名没有throws子句;当一个运行时异常在run方法中抛出,默认的行为就是将异常栈中的信息打印到控制台而后结束程序;ide

幸运的是,Java提供了在线程中捕获和处理运行时异常的机制,从而避免了程序应为运行时异常而终止;spa

接下来展现这种机制;线程

动手实现

(1)  首先须要实现接口Thread.UncaughtExceptionHandler,code

public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.printf("An exception has been captured\n");
        System.out.printf("Thread: %s\n",t.getId());
        System.out.printf("Exception: %s: %s\n",e.getClass().getName(),e.getMessage());
        System.out.printf("Stack Trace: \n");
        e.printStackTrace(System.out);
        System.out.printf("Thread status: %s\n",t.getState());
    }
}
(2)建立线程,触发一个运行时异常

public class Task implements Runnable {
    @Override
    public void run() {
        // trigger runtime exception
        int numero=Integer.parseInt("TTT");
    }
    public static void main(String[] args) {
        Thread thread = new Thread(new Task());

        //Handle runtime exception in thread
        thread.setUncaughtExceptionHandler(new ExceptionHandler());

        thread.start();
    }
}
一次运行结果:

An exception has been captured
Thread: 10
Exception: java.lang.NumberFormatException: For input string: "TTT"
Stack Trace: 
java.lang.NumberFormatException: For input string: "TTT"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at com.sun.base.thread.A_d.Task.run(Task.java:10)
at java.lang.Thread.run(Thread.java:745)
Thread status: RUNNABLEorm

要点

(1)实现接口Thread.UncaughtExceptionHandler对象

(2)为线程设置异常处理属性接口

当一个异常在线程中被抛出,而且没有捕获,JVM会检查这个线程是否设置了对应异常处理Handler,若是设置了JVM调用这个Handler;若是没有,程序打印堆栈信息而后退出;get

线程Thread还有一个方法setDefaultUncaughtExceptionHandler 能够为全部的线程对象设置一个默认的异常处理Handler;
input

当异常在线程中抛出,JVM首先检查对应的线程是否有异常处理handler,若是没有,JVM将会检查ThreadGroup(后面记录)的异常处理Handler,若是还不存在,JVM检查默认的错误处理Handler,若是没有设置,打印堆栈,退出程序;

相关文章
相关标签/搜索