catch的执行与try的匹配
?
這里有一段代碼:
public class EmbededFinally {
??? ?public static void main(String args[]) {
??????? ??int result;
??????? ??try {
??????????? ???System.out.println("in Level 1");
?
?????????? ?? ?try {
??????????????? ????System.out.println("in Level 2");
?????????????????? // result=100/0;? //Level 2
?????????????? ?????try {
?????????????????? ???? ?System.out.println("in Level 3");
????????????????????? ???? ?result=100/0;? //Level 3
??????????????? ????}
??????????????? ????catch (Exception e) {
??????????????????? ?????System.out.println("Level 3:" + e.getClass().toString());
??????????????? ????}
?????????????????????finally {
??????????????????? ?????System.out.println("In Level 3 finally");
??????????????? ????}
????????????????????// result=100/0;? //Level 2
??????????? ????}
????catch (Exception e) {
?????????????? ??? ?System.out.println("Level 2:" + e.getClass().toString());
?????????? ?? ?}
?? ?finally {
??????????????? ????System.out.println("In Level 2 finally");
?????????? ??? }
???????????? ???// result = 100 / 0;? //level 1
??????? ??}
??????? ??catch (Exception e) {
??????????? ???System.out.println("Level 1:" + e.getClass().toString());
??????? ??}
??????? ??finally {
?????????? ?? ?System.out.println("In Level 1 finally");
??????? ??}
??? ?}
}
當我去掉level2中頂部的在level3 try之前的注釋時level3中的catch不執行。
當我去掉level2中底部的在level3之后的注釋時level3中的catch執行。
于是得到,當在try內發現錯誤時則拋出異常,而try內編碼則不繼續向下執行。之后catch接到異常并執行,最后執行同等級finally。
?
轉載于:https://www.cnblogs.com/hehejeson/p/4963828.html
總結
以上是生活随笔為你收集整理的catch的执行与try的匹配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大道至简6章总结
- 下一篇: try、catch、finally的执行