Deligence
2017-02-08 19:01
package com.imooc;
import javax.xml.transform.TransformerException;
public class TestException {
public TestException() {
}
boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
}
boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
System.out.println("ret=="+ret);
//System.out.println("echang=="+e);
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1游戏场, catch exception");
ret = false;
throw e;
} finally {
//System.out.println("异常是");
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
}
boolean testEx2() throws Exception{
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return true;
} /*catch (NullPointerException e) {
System.out.println("testEx2低级异常, catch exception");
ret = false;
//System.out.println("e=="+e);
throw e;
//System.out.println("能不能执行?");
} */finally {
System.out.println("testEx2低级最终要执行, finally; return value=" + ret);
return ret;
}
}
public static void main(String[] args) {
TestException testException1 = new TestException();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
}
}
testEx2()会出异常,但是你把catch注释了,它当然捕获不到,而且因为这是非检查异常,所以系统不会自动抛出,所以定义testEx2() 时后面的throws Exception等于没起到作用,也即上一级的testEx1()和testEx()都不会捕获到异常
main函数里好像没有执行testEx2()...
Java入门第三季
409792 学习 · 4340 问题
相似问题