今天,学习了try-catch-finally语句,本来觉得蛮简单、易懂的。搜了一道相关类型的题。结果信心被泼了盆冷水。先把题mark一下,出去透透风。
1 public class testex { 2 private int c; 3 4 public testex() { 5 } 6 7 @suppresswarnings(finally) 8 boolean testex() throws exception { 9 boolean ret = true;10 try {11 ret = testex1();12 } catch (exception e) {13 system.out.println(testex, catch exception);14 ret = false;15 throw e;16 } finally {17 system.out.println(testex, finally; return value= + ret);18 return ret;19 }20 }21 22 @suppresswarnings(finally)23 boolean testex1() throws exception {24 boolean ret = true;25 try {26 ret = testex2();27 if (!ret) {28 return false;29 }30 system.out.println(testex1, at the end of try);31 return ret;32 } catch (exception e) {33 system.out.println(testex1, catch exception);34 ret = false;35 throw e;36 } finally {37 system.out.println(testex1, finally; return value= + ret);38 return ret;39 }40 }41 42 @suppresswarnings(finally)43 boolean testex2() throws exception {44 boolean ret = true;45 try {46 int b = 12;47 for (int i = 2; i >= -2; i--) {48 setc(b / i);49 system.out.println(i= + i);50 }51 return true;52 } catch (exception e) {53 system.out.println(testex2, catch exception);54 ret = false;55 throw e;56 } finally {57 system.out.println(testex2, finally; return value= + ret);58 return ret;59 }60 }61 62 public static void main(string[] args) {63 testex testexception1 = new testex();64 try {65 testexception1.testex();66 } catch (exception e) {67 e.printstacktrace();68 }69 }70 71 public int getc() {72 return c;73 }74 75 public void setc(int c) {76 this.c = c;77 }78 }
output:
看完之后我就在想,
1.java异常处理机制,我真的理解了吗?
2.java异常处理,我真的掌握了吗?
3.catch体里遇到return 是怎么处理?
4.finally 体里有return怎么处理?
5. catch 和 finally 体里同时遇上 return 怎么办?
6.是不是还有个system.exit()?遇到它又咋办??
7.仅仅知道throws不是完全之策啊,还要继续深入理解啊。
以上就是java异常捕获讲解的详细内容。