日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java中异常与return

發(fā)布時(shí)間:2025/3/15 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中异常与return 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

抽時(shí)間整理了下java中異常與return,以前這塊總是弄混淆,覺得還是寫下來(lái)慢慢整理比較好。由于水平有限,僅供參考。廢話不多說(shuō),直接上代碼。

下面是兩個(gè)方法:

1 public static int throwReturn(){ 2 int ret = 0; 3 try{ 4 ret = 10/0 ; 5 ret++; 6 return ret; 7 }catch(Exception e){ 8 System.out.println("catch block " + e.getMessage()); 9 //ret++; 10 return ret; 11 }finally{ 12 System.out.println("finally block invoked!!!"); 13 ret++; 14 System.out.println("finally block invoked, ret is " + ret); 15 } 16 17 } 1 public static int finallyReturn(){ 2 int ret = 0; 3 try{ 4 ret = 10/0 ; 5 ret++; 6 return ret; 7 }catch(Exception e){ 8 System.out.println("catch block " + e.getMessage()); 9 //ret++; 10 return ret; 11 }finally{ 12 System.out.println("finally block invoked!!!"); 13 ret++; 14 System.out.println("finally block invoked, ret is " + ret); 15 return ret; 16 } 17 18 }

然后在主方法中分別調(diào)用兩個(gè)方法:

1 public static void main(String args[]){ 2 System.out.println("throwReturn:" + throwReturn()); 3 //System.out.println("finallyReturn:" + finallyReturn()); 4 }

第一個(gè)方法輸出結(jié)果:

catch block / by zero finally block invoked!!! finally block invoked, ret is 1 throwReturn:0

throwRetrun方法返回的結(jié)果并不是我預(yù)想的1,而是0。

個(gè)人分析:

  • 程序執(zhí)行到throwReturn方法的第4行時(shí)由于除0而出錯(cuò),程序進(jìn)入catch塊,首先會(huì)執(zhí)行打印輸出:catch block / by zero
  • 接下來(lái)會(huì)執(zhí)行catch塊的return ret語(yǔ)句,碰到return語(yǔ)句方法會(huì)返回退出,而finally語(yǔ)句又是必須執(zhí)行的,此時(shí)程序會(huì)將return的結(jié)果值暫存起來(lái),繼續(xù)執(zhí)行finally塊。
  • 進(jìn)入finally塊后會(huì)輸出:finally block invoked!!! 和finally block invoked, ret is 1
  • finally塊執(zhí)行完成后程序會(huì)回到return處,并返回當(dāng)時(shí)暫存的值
  • 第二個(gè)方法的輸出結(jié)果:

    catch block / by zero finally block invoked!!! finally block invoked, ret is 1 finallyReturn:1

    哎,這次的輸出結(jié)果是1了。

    仔細(xì)比較兩個(gè)方法發(fā)現(xiàn)第二個(gè)方法,在finally語(yǔ)句中多了一個(gè)return ret;程序的執(zhí)行過程同上面基本上是一樣的,只是在最終執(zhí)行finally代碼塊是碰到了return語(yǔ)句,此時(shí)程序就直接將ret的值返回了,而此時(shí)ret的值是1,最后輸出:finallyReturn:1

    接下來(lái)我們?cè)倏?個(gè)方法:

    ?

    1 public static int throwException() throws Exception{ 2 int ret = 0; 3 try{ 4 ret = 10/0 ; 5 System.out.println("ret:" + ret); 6 return ret; 7 }catch(Exception e){ 8 System.out.println("catch block " + e.getMessage()); 9 throw e; 10 }finally{ 11 System.out.println("finally block invoked!!!"); 12 ret++; 13 System.out.println("finally block invoked, ret is " + ret); 14 } 15 16 }

    ?

    1 public static int finallyThrowException() throws Exception{ 2 int ret = 0; 3 try{ 4 ret = 10/0 ; 5 System.out.println("ret:" + ret); 6 return ret; 7 }catch(Exception e){ 8 System.out.println("catch block " + e.getMessage()); 9 throw e; 10 }finally{ 11 System.out.println("finally block invoked!!!"); 12 ret++; 13 System.out.println("finally block invoked, ret is " + ret); 14 return ret; 15 } 16 17 }

    然后在主方法中分別調(diào)用兩個(gè)上面方法:

    ?

    1 public static void main(String args[]){ 2 try { 3 System.out.println("throwException:" + throwException()); 4 } catch (Exception e) { 5 System.out.println("捕獲到throwException方法拋出的異常," + e.getMessage()); 6 } 7 8 /*try { 9 System.out.println("finallyThrowException:" + finallyThrowException()); 10 } catch (Exception e) { 11 System.out.println("捕獲到finallyThrowException方法拋出的異常," + e.getMessage()); 12 }*/ 13 }

    ?

    第一個(gè)方法輸出結(jié)果:

    catch block / by zero finally block invoked!!! finally block invoked, ret is 1 捕獲到throwException方法拋出的異常,/ by zero

    個(gè)人分析:

  • throwException方法執(zhí)行到第4行時(shí),因?yàn)槌?操作拋出異常,程序進(jìn)入catch塊,首先執(zhí)行打印輸出:catch block / by zero
  • 接下來(lái)會(huì)執(zhí)行catch塊的throw e語(yǔ)句,向上拋出異常,而finally語(yǔ)句又是必須執(zhí)行的,此時(shí)程序會(huì)先執(zhí)行finally塊。
  • 進(jìn)入finally塊后會(huì)輸出:finally block invoked!!! 和finally block invoked, ret is 1
  • finally塊執(zhí)行完成后程序會(huì)回到catch塊throw處,將捕獲的異常向上拋出
  • 在main方法中會(huì)捕獲到throwException方法拋出的異常而進(jìn)入catch塊,所以會(huì)輸出:捕獲到throwException方法拋出的異常,/ by zero
  • 第二個(gè)方法的輸出結(jié)果:

    ?

    catch block / by zero finally block invoked!!! finally block invoked, ret is 1 finallyThrowException:1

    ?

    觀察輸出結(jié)果會(huì)發(fā)現(xiàn),主方法并沒有捕獲到finallyThrowException方法調(diào)用時(shí)的異常(catch塊的打印沒有執(zhí)行)。

    這兩個(gè)方法的主要區(qū)別也是在于:在finallyThrowException方法的finally塊中多出了return ret語(yǔ)句。調(diào)用finallyThrowException方法的執(zhí)行過程同調(diào)用throwException方法基本一致。

  • finallyThrowException方法執(zhí)行時(shí),因?yàn)槌?操作拋出異常,程序進(jìn)入catch塊,首先執(zhí)行打印輸出:catch block / by zero
  • 接下來(lái)會(huì)執(zhí)行catch塊的throw e語(yǔ)句,向上拋出異常,而finally語(yǔ)句又是必須執(zhí)行的,此時(shí)程序會(huì)先執(zhí)行finally塊。
  • 進(jìn)入finally塊后會(huì)輸出:finally block invoked!!! 和finally block invoked, ret is 1
  • finally塊執(zhí)行到return ret時(shí),該方法直接返回了ret的值,
  • 在main方法中得到finallyThrowException的返回值后輸出:finallyThrowException:1
  • finallyThrowException方法執(zhí)行結(jié)果可以看出方法執(zhí)行時(shí)的異常被丟失了


    最后再來(lái)看一個(gè)小例子

    1 public static void finallyWork(){ 2 int count = 0; 3 while(true){ 4 try{ 5 if(count++ == 0){ 6 throw new Exception("my error"); 7 } 8 System.out.println("invoked ..."); 9 }catch(Exception e){ 10 System.out.println("catched exception:" + e.getMessage()); 11 }finally{ 12 System.out.println("finally block invoked!!!"); 13 if(count == 2){ 14 break; 15 } 16 } 17 } 18 }

    這個(gè)小例子的主要思路是當(dāng)java中的異常不允許我們回到異常拋出的地點(diǎn)時(shí),我們可以將try塊放到循環(huán)里,這樣程序就又可以回到異常的拋出點(diǎn)了,可以同時(shí)設(shè)置一個(gè)計(jì)數(shù)器,當(dāng)累積嘗試一定的次數(shù)后程序就退出。

    ok,就說(shuō)這么多了,下面附上完整代碼:

    package tt;public class FinallyWorks {/*** @param args*/public static void main(String[] args) {//finallyWork();/**<output begin>* catch block / by zero* finally block invoked!!!* finally block invoked, ret is 1* throwReturn:0*</output end>*從輸出結(jié)果中可以看出程序在int temp = 10/0;這一行拋出異常,直接進(jìn)入catch塊,首先輸出打印catch block...,繼續(xù)往下執(zhí)行時(shí)碰到return語(yǔ)句,由于程序*存在finally語(yǔ)句,在程序返回之前需要執(zhí)行finally語(yǔ)句。那么此時(shí)程序會(huì)將return的結(jié)果值暫時(shí)存起來(lái),繼續(xù)執(zhí)行finally,從輸出上可以看出finally執(zhí)行后ret*的值變?yōu)榱?,而整個(gè)方法最終的返回結(jié)果是0,說(shuō)明return的是之前暫存的值。* *///System.out.println("throwReturn:" + throwReturn());/** <output begin>* catch block / by zero* finally block invoked!!!* finally block invoked, ret is 1* finallyReturn:1*</output end>*從輸出結(jié)果中可以看出程序在int temp = 10/0;這一行拋出異常,直接進(jìn)入catch塊,首先輸出打印catch block...,繼續(xù)往下執(zhí)行時(shí)碰到return語(yǔ)句,由于程序*存在finally語(yǔ)句,在程序返回之前需要執(zhí)行finally語(yǔ)句。那么此時(shí)程序會(huì)將return的結(jié)果值暫時(shí)存起來(lái),繼續(xù)執(zhí)行finally,從輸出上可以看出finally執(zhí)行后ret*的值變?yōu)榱?,有在finally塊中碰到了return語(yǔ)句,方法就直接返回了,所以方法結(jié)果返回了1。* *///System.out.println("finallyReturn:" + finallyReturn());/**<output begin>* catch block / by zero* finally block invoked!!!* finally block invoked, ret is 1* 捕獲到throwException方法拋出的異常,/ by zero*</output end>*從輸出結(jié)果中可以看出在調(diào)用throwException方法是出現(xiàn)異常,程序進(jìn)入該方法的catch塊中,輸出:catch block / by zero*由于存在finally,程序會(huì)先執(zhí)行完finally語(yǔ)句輸出:finally block invoked!!! 和 finally block invoked, ret is 1*然后將捕獲到的異常拋向上層。上層的main方法catch到這個(gè)異常之后會(huì)輸出:捕獲到throwException方法拋出的異常,/ by zero*《注意throwException:那句打印是不會(huì)輸出的》* *//*try {System.out.println("throwException:" + throwException());} catch (Exception e) {System.out.println("捕獲到throwException方法拋出的異常," + e.getMessage());}*//**<output begin>* catch block / by zero* finally block invoked!!!* finally block invoked, ret is 1* finallyThrowException:1*</output end>*從輸出結(jié)果中可以看出在調(diào)用finallyThrowException方法是出現(xiàn)異常,程序進(jìn)入該方法的catch塊中,輸出:catch block / by zero*由于存在finally,程序會(huì)先執(zhí)行完finally語(yǔ)句輸出:finally block invoked!!! 和 finally block invoked, ret is 1*之后程序執(zhí)行到finally塊中return語(yǔ)句,直接返回了ret的值,主方法接受到這個(gè)返回值后輸出:finallyThrowException:1*《注意主方法中catch塊代碼并沒有被執(zhí)行,這就說(shuō)明了finallyThrowException方法中異常被丟失了》* */try {System.out.println("finallyThrowException:" + finallyThrowException());} catch (Exception e) {System.out.println("捕獲到finallyThrowException方法拋出的異常," + e.getMessage());}}public static int throwException() throws Exception{int ret = 0;try{ret = 10/0 ;System.out.println("ret:" + ret);return ret;}catch(Exception e){System.out.println("catch block " + e.getMessage());throw e;}finally{System.out.println("finally block invoked!!!");ret++;System.out.println("finally block invoked, ret is " + ret);}}public static int finallyThrowException() throws Exception{int ret = 0;try{ret = 10/0 ;System.out.println("ret:" + ret);return ret;}catch(Exception e){System.out.println("catch block " + e.getMessage());throw e;}finally{System.out.println("finally block invoked!!!");ret++;System.out.println("finally block invoked, ret is " + ret);return ret;}}public static int throwReturn(){int ret = 0;try{ret = 10/0 ;ret++;return ret;}catch(Exception e){System.out.println("catch block " + e.getMessage());//ret++;return ret;}finally{System.out.println("finally block invoked!!!");ret++;System.out.println("finally block invoked, ret is " + ret);}}public static int finallyReturn(){int ret = 0;try{ret = 10/0 ;ret++;return ret;}catch(Exception e){System.out.println("catch block " + e.getMessage());//ret++;return ret;}finally{System.out.println("finally block invoked!!!");ret++;System.out.println("finally block invoked, ret is " + ret);return ret;}}/*** 當(dāng)java中的異常不允許我們回到異常拋出的地點(diǎn)時(shí),我們可以將try塊放到循環(huán)里,* 這樣程序就又可以回到異常的拋出點(diǎn)了,可以同時(shí)設(shè)置一個(gè)計(jì)數(shù)器,* 當(dāng)累積嘗試一定的次數(shù)后程序就退出。*/public static void finallyWork(){int count = 0;while(true){try{if(count++ == 0){throw new Exception("my error");}System.out.println("invoked ...");}catch(Exception e){System.out.println("catched exception:" + e.getMessage()); }finally{System.out.println("finally block invoked!!!");if(count == 2){break;}}}}}

    ?

    ?

    ?

    ?

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/pengkw/archive/2012/11/22/2783342.html

    總結(jié)

    以上是生活随笔為你收集整理的java中异常与return的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。