尝试将两个知识合并起来写段代码,调试遇到问题,把这个这么简单的问题提到这里请不吝赐教,谢谢!
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);
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
excepTest方法中抛出异常,然后该方法上抛了异常该异常被抛给了run方法,应该在run方法内处理该问题
但是代码中的查找捕获却在main方法中,肯定捕获不到异常
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();
}
}