代码改变世界

运行时异常RuntimeException捕获的小测试

2015-06-25 09:42  Loull  阅读(1274)  评论(0)    收藏  举报
public class ExceptionTest {

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {// ///
                    @Override
                    public void run() {
                        // /疑问点:1、神锋说子线程不会出现堆栈错误 2、有人说整个线程都去catch一下(我觉得没意义)?
                        // //异常捕抓了,程序会继续往下走。
                        // //运行时异常不用显示去补抓,子线程出现运行时异常时,会打印出堆栈错误,子线程停止,main线程没影响
                        // //补抓throwable 有啥意义?让它继续走?
                        // //子线程出现runtimeexception时不捕获,会让父线程死吗(我测试了不会)?
                        //////捕获一些未知的异常的意义在于,出现了异常,进行处理,不影响后面代码的进行
                        try {
                            byte[] b = new byte[2];
                            System.out.println(b[3]);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        while (true) {
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            System.out
                                    .println(Thread.currentThread().getName());
                        }
                    }

                }).start();
        // ///
        while (true) {
            Thread.sleep(3000);
            System.out.println(Thread.currentThread().getName());
        }
    }
}