菜鸟关于Java中抛出异常和上抛异常的问题
阿神
阿神 2017-04-17 14:27:39
0
4
384

本人最近在学Java,学到线程和异常处理
尝试将两个知识合并起来写段代码,调试遇到问题,搞不懂
某某知道提问基本没人理,所以才把这个这么简单的问题提到这里
请不吝赐教,谢谢!

class Test22_05 implements Runnable{

    public void run() {
        for(int i = 0; i < 10; i++){
            this.excepTest(i);
            System.out.println(Thread.currentThread().getName() + ":i = " + i);
        }
    }

    public void excepTest(int i)throws Exception{
        if(i == 8){
                throw new Exception("这是手动抛出异常!");
            }
    }
}

public class JavaTest22_05{
    public static void main(String args[]){
        Test22_05 t1 = new Test22_05();
        Thread tt1 = new Thread(t1);
        Thread tt2 = new Thread(t1);
        Thread tt3 = new Thread(t1);
        tt1.setName("线程1");
        tt2.setName("线程2");
        tt3.setName("线程3");
        try{
            tt1.start();
            tt2.start();
            tt3.start();
        }catch(Exception e){
            System.out.println(e);
        }
    }
}



阿神
阿神

闭关修行中......

reply all(4)
阿神

I found the problem, I threw an exception in the excepTest method, and then the exception was thrown on the method

The exception was thrown to the run method and the problem should be handled within the run method

But the search capture in the code is in the main method, so the exception will definitely not be caught

Corrected code:

class Test22_05 implements Runnable{

    public void run() {
        for(int i = 0; i < 10; i++){
            try{
                this.excepTest(i);
            }catch(Exception e){
                System.out.println(Thread.currentThread().getName() + "出现异常:" + e);
            }
            System.out.println(Thread.currentThread().getName() + ":i = " + i);
        }
    }

    public void excepTest(int i)throws Exception{
        if(i == 8){
                throw new Exception("这是手动抛出异常!");
            }
    }
}

public class JavaTest22_05{
    public static void main(String args[]){
        Test22_05 t1 = new Test22_05();
        Thread tt1 = new Thread(t1);
        Thread tt2 = new Thread(t1);
        Thread tt3 = new Thread(t1);
        tt1.setName("线程1");
        tt2.setName("线程2");
        tt3.setName("线程3");
            tt1.start();
            tt2.start();
            tt3.start();
            tt1.start(); //本行将报错,因为线程只能启动一次
    }
}

//本例中是将上抛异常和捕获异常写到多线程代码里面
//目的是实现当i为8时手动抛出异常
//基本上实现了,但是不知道为什么,三个线程处理了30条记录,不符合Runnable接口的共享资源特征
//将在JavaTest22_06.java中重写程序,试图找出问题。



黄舟

Other threads cannot throw exceptions directly. If you think about it, the thread itself is asynchronous. When you execute the thread, the main thread's method has already finished running

PHPzhong

If you use an IDE tool, it will definitely prompt you this.excepTest(i); Unhandled exception type Exception

刘奇

I don’t understand what you want to ask. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!