一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - 深入剖析Java中的各種異常處理方式

深入剖析Java中的各種異常處理方式

2019-12-31 14:44zinss26914 JAVA教程

這篇文章主要介紹了深入剖析Java中的各種異常處理方式,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

1. 調(diào)試追蹤代碼:

?
1
2
3
4
5
6
7
8
9
10
11
public static void enterTryMethod() {
  System.out.println("enter after try field");
}
 
public static void enterExceptionMethod() {
  System.out.println("enter catch field");
}
 
public static void enterFinallyMethod() {
  System.out.println("enter finally method");
}

2. 拋出Exception,沒有finally,當(dāng)catch遇上return

   

?
1
2
3
4
5
6
7
8
9
10
11
12
public static int catchTest() {
    int res = 0;
     
    try {
      res = 10 / 0; // 拋出Exception,后續(xù)處理被拒絕
      enterTryMethod();
      return res; // Exception已經(jīng)拋出,沒有獲得被執(zhí)行的機(jī)會(huì)
    } catch (Exception e) {
      enterExceptionMethod();
      return 1// Exception拋出,獲得了調(diào)用方法并返回方法值的機(jī)會(huì)
    }
  }

后臺(tái)輸出結(jié)果:

?
1
2
enter catch field
1

3. 拋出Exception,當(dāng)catch體里有return,finally體的代碼塊將在catch執(zhí)行return之前被執(zhí)行

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static int catchTest() {
    int res = 0;
     
    try {
      res = 10 / 0; // 拋出Exception,后續(xù)處理被拒絕
      enterTryMethod();
      return res; // Exception已經(jīng)拋出,沒有獲得被執(zhí)行的機(jī)會(huì)
    } catch (Exception e) {
      enterExceptionMethod();
      return 1// Exception拋出,獲得了調(diào)用方法并返回方法值的機(jī)會(huì)
    } finally {
      enterFinallyMethod(); // Exception拋出,finally代碼將在catch執(zhí)行return之前被執(zhí)行
    }
  }

后臺(tái)輸出結(jié)果:

?
1
2
3
enter catch field
enter finally method
1

4. 不拋出Exception,當(dāng)finally代碼塊里面遇上return,finally執(zhí)行完后將結(jié)束整個(gè)方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static int catchTest() {
  int res = 0;
   
  try {
    res = 10 / 2; // 不拋出Exception
    enterTryMethod();
    return res; // 獲得被執(zhí)行的機(jī)會(huì),但執(zhí)行需要在finally執(zhí)行完成之后才能被執(zhí)行
  } catch (Exception e) {
    enterExceptionMethod();
    return 1;
  } finally {
    enterFinallyMethod();
    return 1000; // finally中含有return語句,這個(gè)return將結(jié)束這個(gè)方法,不會(huì)在執(zhí)行完之后再跳回try或者catch繼續(xù)執(zhí)行,方法到此結(jié)束
  }
}

后臺(tái)輸出結(jié)果:

?
1
2
3
enter after try field
enter finally method
1000

5. 不拋Exception,當(dāng)finally代碼塊里面遇上System.exit()方法將結(jié)束和終止整個(gè)程序,而不只是方法

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static int catchTest() {
  int res = 0;
   
  try {
    res = 10 / 2; // 不拋出Exception
    enterTryMethod();
    return res; // 獲得被執(zhí)行的機(jī)會(huì),但由于finally已經(jīng)終止程序,返回值沒有機(jī)會(huì)被返回
  } catch (Exception e) {
    enterExceptionMethod();
    return 1;
  } finally {
    enterFinallyMethod();
    System.exit(0); // finally中含有System.exit()語句,System.exit()將退出整個(gè)程序,程序?qū)⒈唤K止
  }
}

后臺(tái)輸出結(jié)果:

?
1
2
enter after try field
enter finally method

6. 拋出Exception,當(dāng)catch和finally同時(shí)遇上return,catch的return返回值將不會(huì)被返回,finally的return語句將結(jié)束整個(gè)方法并返回

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static int catchTest() {
   int res = 0;
    
   try {
     res = 10 / 0; // 拋出Exception,后續(xù)處理將被拒絕
     enterTryMethod();
     return res; // Exception已經(jīng)拋出,沒有獲得被執(zhí)行的機(jī)會(huì)
   } catch (Exception e) {
     enterExceptionMethod();
     return 1; // Exception已經(jīng)拋出,獲得被執(zhí)行的機(jī)會(huì),但返回操作將被finally截?cái)?
   } finally {
     enterFinallyMethod();
     return 10; // return將結(jié)束整個(gè)方法,返回值為10
   }
 }

后臺(tái)輸出結(jié)果:

?
1
2
3
enter catch field
enter finally method
10

7. 不拋出Exception,當(dāng)finally遇上return,try的return返回值將不會(huì)被返回,finally的return語句將結(jié)束整個(gè)方法并返回

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static int catchTest() {
   int res = 0;
    
   try {
     res = 10 / 2; // 不拋出Exception
     enterTryMethod();
     return res; // 獲得執(zhí)行機(jī)會(huì),但返回將被finally截?cái)?
   } catch (Exception e) {
     enterExceptionMethod();
     return 1;
   } finally {
     enterFinallyMethod();
     return 10; // return將結(jié)束整個(gè)方法,返回值為10
   }
 }

后臺(tái)輸出結(jié)果:

?
1
2
3
enter after try field
enter finally method
10


結(jié)論
Java的異常處理中,程序執(zhí)行完try里面的代碼塊之后,該方法并不會(huì)立即結(jié)束,而是繼續(xù)試圖去尋找該方法有沒有finally的代碼塊

    如果沒有finally代碼塊,整個(gè)方法在執(zhí)行完try代碼塊后返回相應(yīng)的值來結(jié)束整個(gè)方法
    如果有finally代碼塊,此時(shí)程序執(zhí)行到try代碼塊里的return一句之時(shí)并不會(huì)立即執(zhí)行return,而是先去執(zhí)行finally代碼塊里的代碼

若finally代碼塊里沒有return或沒有能夠終止程序的代碼,程序在執(zhí)行完finally代碼塊代碼之后再返回try代碼塊執(zhí)行return語句來結(jié)束整個(gè)方法。若 finally 代碼塊里有 return 或含有能夠終止程序的代碼,方法將在執(zhí)行完 finally 之后被結(jié)束,不再跳回 try 代碼塊執(zhí)行 return
在拋出異常的情況下,原理也是和上面的一樣的,你把上面說到的 try 換成 catch 去理解就OK了。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片在线免费视频 | 高清毛片aaaaaaaaa片 | 性xxxx18学生第一次出血 | 91青青国产在线观看免费 | 久9青青cao精品视频在线 | 国产专区视频在线观看 | 色婷在线 | 高清色黄毛片一级毛片 | 日本久久啪啪婷婷激情五月 | 日韩免费毛片视频杨思敏 | 亚洲精品成人456在线播放 | 亚洲精品一线二线三线 | 美女gif趴跪式抽搐动态图 | 天堂网站天堂小说 | 九九精品影院 | 99视频有精品 | 成年人在线免费看 | 男人天堂视频网 | 午夜小视频免费观看 | 国产高清在线播放刘婷91 | 91手机看片国产永久免费 | 男男羞羞视频网站国产 | 国产精品激情综合久久 | 国产成人亚洲精品91专区手机 | 国产成人v爽在线免播放观看 | 男人的天堂va | ady久久 | 日比免费视频 | 亚洲日韩中文字幕一区 | hezyo加勒比一区二区三区 | 亚洲乱码一二三四五六区 | 欧美在线观看网址 | 91超级碰| 跪在老师脚下吃丝袜脚 | 9l国产精品久久久久麻豆 | 欧美一区精品二区三区 | 国产亚洲sss在线观看 | 第一福利在线导航 | 4438全国最大免费观看 | 精品久久久久久久久久久 | 国产综合图区 |