今天,学习了try-catch-finally语句,本来觉得蛮简单、易懂的。搜了一道相关类型的题。结果信心被泼了盆冷水。先把题Mark一下,出去透透风。
public class TestEx {
private int c; public TestEx() {
} @SuppressWarnings("finally")
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;
}
} @SuppressWarnings("finally")
boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
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("testEx1, finally; return value=" + ret);
return ret;
}
} @SuppressWarnings("finally")
boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
for (int i = 2; i >= -2; i--) {
setC(b / i);
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
} public static void main(String[] args) {
TestEx testException1 = new TestEx();
try {
testException1.testEx();
} catch (Exception e) {
e.printStackTrace();
}
} public int getC() {
return c;
} public void setC(int c) {
this.c = c;
}
}
Output:
看完之后我就在想,
1.Java异常处理机制,我真的理解了吗?
2.Java异常处理,我真的掌握了吗?
3.catch体里遇到return 是怎么处理?
4.finally 体里有return怎么处理?
5. catch 和 finally 体里同时遇上 return 怎么办?
6.是不是还有个System.exit()?遇到它又咋办??
7.仅仅知道throws不是完全之策啊,还要继续深入理解啊。2017-06-1918:21:08