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

来源:互联网 发布:程序员客栈怎么接单 编辑:程序博客网 时间:2024/05/23 12:54

处理线程不受控制的异常

在Java中有两种异常:

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

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

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

接下来展示这种机制;

动手实现

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

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: RUNNABLE

要点

(1)实现接口Thread.UncaughtExceptionHandler

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

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

线程Thread还有一个方法setDefaultUncaughtExceptionHandler 可以为所有的线程对象设置一个默认的异常处理Handler;

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


0 0
原创粉丝点击