前言:
java 中的異常處理機(jī)制你真的理解了嗎?掌握了嗎?
catch 體里遇到 return 是怎么處理? finally 體遇到 return 怎么辦?finally 體里有 System.exit() 方法怎么處理?當(dāng) catch 和 finally 體里同時(shí)遇上 return 怎么辦?
相信你在處理異常的時(shí)候不是每次都把它 throws 掉就完事了,很多時(shí)候異常是需要我們自己來 catch 并針對(duì)所拋出的 Exception 做一些后續(xù)的處理工作。
直接上代碼,先貼下面測(cè)試需要調(diào)用的方法:
1
2 // catch 后續(xù)處理工作
3 public static boolean catchMethod() {
4 System.out.print("call catchMethod and return --->> ");
5 return false;
6 }
7 // finally后續(xù)處理工作
8 public static void finallyMethod() {
9 System.out.println();
10 System.out.print("call finallyMethod and do something --->> ");
11 }
12 1. 拋出 Exception,沒有 finally,當(dāng) catch 遇上 return
1
2public static boolean catchTest() {
3 try {
4 int i = 10 / 0; // 拋出 Exception,后續(xù)處理被拒絕
5 System.out.println("i vaule is : " + i);
6 return true; // Exception 已經(jīng)拋出,沒有獲得被執(zhí)行的機(jī)會(huì)
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod(); // Exception 拋出,獲得了調(diào)用方法并返回方法值的機(jī)會(huì)
10 }
11 }
12 后臺(tái)輸出結(jié)果:
1
2 -- Exception --
3call catchMethod and return --->> false
4 2. 拋出 Exception,當(dāng) catch 體里有 return,finally 體的代碼塊將在 catch 執(zhí)行 return 之前被執(zhí)行
1
2public static boolean catchFinallyTest1() {
3 try {
4 int i = 10 / 0; // 拋出 Exception,后續(xù)處理被拒絕
5 System.out.println("i vaule is : " + i);
6 return true; // Exception 已經(jīng)拋出,沒有獲得被執(zhí)行的機(jī)會(huì)
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod(); // Exception 拋出,獲得了調(diào)用方法的機(jī)會(huì),但方法值在 finally 執(zhí)行完后才返回
10 }finally{
11 finallyMethod(); // Exception 拋出,finally 代碼塊將在 catch 執(zhí)行 return 之前被執(zhí)行
12 }
13 }
14 后臺(tái)輸出結(jié)果:
1
2 -- Exception --
3call catchMethod and return --->>
4call finallyMethod and do something --->> false
5 3. 不拋 Exception,當(dāng) finally 代碼塊里面遇上 return,finally 執(zhí)行完后將結(jié)束整個(gè)方法
1
2public static boolean catchFinallyTest2() {
3 try {
4 int i = 10 / 2; // 不拋出 Exception
5 System.out.println("i vaule is : " + i);
6 return true; // 獲得被執(zhí)行的機(jī)會(huì),但執(zhí)行需要在 finally 執(zhí)行完成之后才能被執(zhí)行
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return catchMethod();
10 }finally{
11 finallyMethod();
12 return false; // finally 中含有 return 語(yǔ)句,這個(gè) return 將結(jié)束這個(gè)方法,不會(huì)在執(zhí)行完之后再跳回 try 或 catch 繼續(xù)執(zhí)行,方法到此結(jié)束,返回 false
13 }
14 }
15 后臺(tái)輸出結(jié)果:
1
2i vaule is : 5
3
4call finallyMethod and do something --->> false
5 4. 不拋 Exception,當(dāng) finally 代碼塊里面遇上 System.exit() 方法 將結(jié)束和終止整個(gè)程序,而不只是方法
1
2public static boolean finallyExitTest() {
3 try {
4 int i = 10 / 2; // 不拋出 Exception
5 System.out.println("i vaule is : " + i);
6 return true; // 獲得被執(zhí)行的機(jī)會(huì),但由于 finally 已經(jīng)終止程序,返回值沒有機(jī)會(huì)被返回
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true;
10 }finally {
11 finallyMethod();
12 System.exit(0);// finally 中含有 System.exit() 語(yǔ)句,System.exit() 將退出整個(gè)程序,程序?qū)⒈唤K止
13 }
14 }
15 后臺(tái)輸出結(jié)果:
1
2i vaule is : 5
3
4call finallyMethod and do something --->>
5 5. 拋出 Exception,當(dāng) catch 和 finally 同時(shí)遇上 return,catch 的 return 返回值將不會(huì)被返回,finally 的 return 語(yǔ)句將結(jié)束整個(gè)方法并返回
1
2public static boolean finallyTest1() {
3 try {
4 int i = 10 / 0; // 拋出 Exception,后續(xù)處理被拒絕
5 System.out.println("i vaule is : " + i);
6 return true; // Exception 已經(jīng)拋出,沒有獲得被執(zhí)行的機(jī)會(huì)
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true; // Exception 已經(jīng)拋出,獲得被執(zhí)行的機(jī)會(huì),但返回操作將被 finally 截?cái)?/span>
10 }finally {
11 finallyMethod();
12 return false; // return 將結(jié)束整個(gè)方法,返回 false
13 }
14 }
15 后臺(tái)輸出結(jié)果:
1
2 -- Exception --
3
4call finallyMethod and do something --->> false
5 6. 不拋出 Exception,當(dāng) finally 遇上 return,try 的 return 返回值將不會(huì)被返回,finally 的 return 語(yǔ)句將結(jié)束整個(gè)方法并返回
1
2public static boolean finallyTest2() {
3 try {
4 int i = 10 / 2; // 不拋出 Exception
5 System.out.println("i vaule is : " + i);
6 return true; // 獲得被執(zhí)行的機(jī)會(huì),但返回將被 finally 截?cái)?/span>
7 } catch (Exception e) {
8 System.out.println(" -- Exception --");
9 return true;
10 }finally {
11 finallyMethod();
12 return false; // return 將結(jié)束這個(gè)方法,不會(huì)在執(zhí)行完之后再跳回 try 或 catch 繼續(xù)執(zhí)行,返回 false
13 }
14 }
15 后臺(tái)輸出結(jié)果:
1
2i vaule is : 5
3
4call finallyMethod and do something --->> false
5 結(jié)語(yǔ):
(假設(shè)方法需要返回值)
java 的異常處理中,
在不拋出異常的情況下,程序執(zhí)行完 try 里面的代碼塊之后,該方法并不會(huì)立即結(jié)束,而是繼續(xù)試圖去尋找該方法有沒有 finally 的代碼塊,
如果沒有 finally 代碼塊,整個(gè)方法在執(zhí)行完 try 代碼塊后返回相應(yīng)的值來結(jié)束整個(gè)方法;
如果有 finally 代碼塊,此時(shí)程序執(zhí)行到 try 代碼塊里的 return 語(yǔ)句之時(shí)并不會(huì)立即執(zhí)行 return,而是先去執(zhí)行 finally 代碼塊里的代碼,
若 finally 代碼塊里沒有 return 或沒有能夠終止程序的代碼,程序?qū)⒃趫?zhí)行完 finally 代碼塊代碼之后再返回 try 代碼塊執(zhí)行 return 語(yǔ)句來結(jié)束整個(gè)方法;
若 finally 代碼塊里有 return 或含有能夠終止程序的代碼,方法將在執(zhí)行完 finally 之后被結(jié)束,不再跳回 try 代碼塊執(zhí)行 return。
在拋出異常的情況下,原理也是和上面的一樣的,你把上面說到的 try 換成 catch 去理解就 OK 了 *_*
[ 隨筆均原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:http://www.blogjava.net/fancydeepin ]
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。