public class Test {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
try {
if (i % 3 == 0)
throw new Exception("E0");
System.out.println(i);
} catch (Exception inner) {
i *= 2;
if (i % 3 == 0)
try {
throw new Exception("E1");//这个异常需要catch,否则报错,退出循环!
} catch (Exception e) {
//此处可自定义处理代码
}
} finally {
}
}
}
}
输出结果如下:
1
2
7
8作者: 我自信我很牛 时间: 2013-3-21 22:36
class Demo2{
public static void main(String[] args) {
for(int i=0; i<10; ++i){
try{
if(i%3 == 0) throw new Exception("E0");
System.out.println(i);
} catch (Exception inner){
try
{
i *= 2;
if(i%3 == 0) throw new Exception("E1");
}
catch (Exception e)
{
continue;
}
}finally{
++i;
}
}
}
}首先你写代码还是缩进一下,看晕了,把里面嵌套的异常捕捉了就能运行了,这是我改了以后的结果,运行成功了,你开始把那个异常抛给虚拟机,所以虚拟机就报告错误了,你自己捕捉了就可以了 作者: kingsummerlucky 时间: 2013-3-22 18:01
把代码改成这样,就OK了
public class Test {
public static void main(String[] args) throws Exception {
for (int i = 0; i < 10; i++) {
try {
if (i % 3 == 0)
throw new Exception("有异常出现");
System.out.println(i);
} catch (Exception inner) {
i *= 2;
if (i % 3 == 0)
try {
throw new Exception("E1");
} catch (Exception e) {
throw new Exception(e); }
} finally {
}
}
}
}作者: 黄杨 时间: 2013-3-22 23:08
原来是这样,谢谢大家了!但是我不会修改为已解决,谁懂啊?作者: 黄玉昆 时间: 2013-3-22 23:12