多线程之捕获异常(五)

来源:互联网 发布:iptables端口转发 编辑:程序博客网 时间:2024/05/23 02:03

捕获异常

/** * 线程中发生异常,如果内部没有捕获,外部是无法捕获的 * 因为线程中没有抛出异常 * */public class ThreadException {    public static void main(String[] args) {        try {            Thread t= new Thread("TTTT"){                @Override                public void run() {                    int i = 2/0;                }            };            //捕获线程中的异常            t.setUncaughtExceptionHandler((thread,e)->{                e.printStackTrace();                System.out.println("---------"+ thread);            });            t.start();        } catch (Exception e) {            System.out.println("发生也异常");            e.printStackTrace();        }    }}

wait和sleep区别

这里写图片描述

原创粉丝点击