有關try..catch..finally處理異常的總結

//看一下下面的程序,你能正確的寫出不同的testEx2()方法時,程序的最終打印出來的數據嗎....先不要看下面的答案
public class ExceptionTest {  public ExceptionTest() {  }  boolean testEx() throws Exception {  boolean ret = true;  try {  ret = testEx1();  } catch (Exception e) {  System.out.println("testEx, catch exception");  ret = false;  throw e;  } finally {  System.out.println("testEx, finally; return value=" + ret);  return ret;  }  }  boolean testEx1(){  boolean ret = true;  try {  ret = testEx2(); if(!ret){return false;}System.out.println("testEx1, at the end of try");  return ret;} catch (Exception e) {  System.out.println("testEx1, catch exception");  ret = false;  throw e;  } finally {  System.out.println("testEx1, finally; return value=" + ret);  return ret;  }  }  //第一種:/* boolean testEx2() throws Exception{  boolean ret = true;  try {  int b = 12;  int c;  for (int i = 2; i >= -2; i--) {  c = b / i;System.out.println("i=" + i);  }  return ret;  } catch (Exception e) {  System.out.println("testEx2, catch exception");  ret = false;  throw e;} finally {  System.out.println("testEx2, finally; return value=" + ret);  return ret;  } } *///第二種:boolean testEx2() throws Exception {  boolean ret = true;   int b = 12;  int c;  for (int i = 2; i >= -2; i--) {  c = b / i;  System.out.println("i=" + i);  }  System.out.printf("這句話打打出來了嗎??????");return true;  }//第三種:/*boolean testEx2() throws Exception{  boolean ret = true;  try {  int b = 12;  int c;  for (int i = 2; i >= -2; i--) {  c = b / i;System.out.println("i=" + i);  }  return ret;  } catch (Exception e) {  System.out.println("testEx2, catch exception");  ret = false;  throw e;} finally {  System.out.println("testEx2, finally; return value=" + ret);  //return ret;  //此處不寫return 語句} //System.out.println("fhsdfsdofi");//因為try中有一個直接return語句,所以這兩句不能被訪問//return ret;} *///第四種:/*boolean testEx2() throws Exception{  boolean ret = true;  try {  int b = 12;  int c;  for (int i = 2; i >= -2; i--) {  c = b / i;System.out.println("i=" + i);  }   } catch (Exception e) {  System.out.println("testEx2, catch exception");  //ret = false;  throw e;} finally {  System.out.println("testEx2, finally; return value=" + ret);  //return ret;  //此處不寫return 語句} System.out.println("這句話打印出來了!!!!");return ret;} *///第五種:/*boolean testEx2() throws Exception{  //提醒一下,第四種和第五種只有catch中有沒有 throw e 不一樣boolean ret = true;  try {  int b = 12;  int c;  for (int i = 2; i >= -2; i--) {  c = b / i;System.out.println("i=" + i);  }  //return ret;  } catch (Exception e) {  System.out.println("testEx2, catch exception");  ret = false;  //throw e;} finally {  System.out.println("testEx2, finally; return value=" + ret);  //return ret;  //此處不寫return 語句} System.out.println("這句話打印出來了!!!!!!!");return ret;}*/public static void main(String[] args) {  ExceptionTest testException1 = new ExceptionTest();  try {  testException1.testEx();  } catch (Exception e) {  e.printStackTrace();  }  }  
}  class myException extends Exception{public void printException(){System.out.println("產生異常!");}
}/*異常看著容易,理解起來也很容易!但是它真的沒有你想像的那么簡單!
第一種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false第二種:
i=2
i=1
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false第三種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false第四種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=true
testEx1, catch exception
testEx1, finally; return value=false
testEx, finally; return value=false第五種:
i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
這句話打印出來了!!!!!!!
testEx1, finally; return value=false
testEx, finally; return value=false總結一下:
一:throw new Exception()第一種情形:(由第二種情形驗證)int test() throws Exception{if(....)throw new Exception();....return x;}第二中情形:第五種情況可驗證int test() throws Exception{try{if(...)throw new Exception();}catch(ArithmeticException e){//在try中產生的異常沒有被捕獲時:....}finally{....}.....return x;}在執行到throw 時,第一種先將return返回個調用者(也就是throw和return之間的語句不會被執行),然后再調用程序中尋找處理異常的程序第二種還要將 finally中的語句執行完(即異常有沒有被立即處理都要執行finally),(throw和return之間的語句也不會被執行),然后執行return語句,程序轉向調用者中尋找異常處理程序。
二:再讓我們看一下finally中寫return的一些缺憾
1 finally塊中的return語句會覆蓋try塊、catch塊中的return語句
2 如果finally塊中包含了return語句,即使前面的catch塊重新拋出了異常,則調用該方法的語句也不會獲得catch塊重新拋出的異常,
而是會得到finally塊的返回值,并且不會捕獲異常,也就是如果在catch或者try中產生的異常如果在向外界拋出是不可能的。。。。第一種情況:testEx2()方法中會產生一個ArithmeticException的異常, Exception是它的父類,我們在該方法中捕獲了該異常并進行了處理
所以會輸出 testEx2()中的 catch 和 finally 中的輸出數據, 而在testEx1()方法中,調用了testEx2(),由于testEx2()中的異常已經被處理
并且由于finally中的return語句導致testEx2()中catch中的throw e 無法重新拋出,所以在testEx1()中不會被捕獲。再說一下第四,第五種情況:fianlly中都沒有return
在第四種情況中, 由于在catch中繼續拋出了該異常,該異常在testEx2()中沒有被處理,所以在執行finally之后的語句(除了return語句)不會被執行,
而第五種是沒有繼續拋出該異常,也就是textEx2()中產生的異常全部被處理了,所以finally之后的語句會被執行.....其他不多說。如果還是沒有搞懂的話,推薦看一下下面這個鏈接,寫的不錯....
http://blog.csdn.net/hguisu/article/details/6155636
*/

  

轉載于:https://www.cnblogs.com/hujunzheng/p/3817703.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/531941.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/531941.shtml
英文地址,請注明出處:http://en.pswp.cn/news/531941.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

oracle key的含義,v$session SERIAL#字段的含義

liyx:#!/bin/bash||#Write by liyx||||#數據庫服務器地址||DBHOSTlocalhost||#數據庫登錄名||USERNAMEroot||#數據庫密碼||PASSWORDroot||#需要備份的數據庫 或 輸入類似 db1 db2 的列表清單 例 DBNAMES"all"||DBNAMES"ess_simple"||#備份MYSQL…

java.util.Scanner簡單應用

import java.util.Scanner; import java.io.*; public class FileScannerTest{public static void main(String args[]){ //**************Scanner 的一般用//1.public Scanner(InputStream source),利用InputStream 對象進行構造Scanner myScanner1 new Scanner(System.in);w…

oracle能查dml記錄么,如何查詢DML操作的詳細記錄

可以通過flashback_transaction_qurey視圖查詢eg:SQL> desc flashback_transaction_queryName Null? Type----------------------------------------- -------- ----------------------------XID …

krpano 場景切換 通知_一個基于Vulkan的異步場景加載設計

異步場景加載基本流程驗證完成。此方法理論上只需要使用3個Vulkan的指令隊列。對于移動平臺上的Vulkan,指令隊列數量極少,比如Adreno640只有3個指令隊列可用。所以理論上這一設計也適合目前的移動平臺使用。(1) graphic_queue:用于完成當前場…

oracle 數據庫回閃,各種數據庫閃回的總結

本帖最后由 guoyJoe 于 2013-3-26 21:15 編輯一、Fashback Query閃回查詢:Books-->APP-->Application Developers Guide - Fundamentals-->Flashback1、應用Flashback Query查詢過去的數據select * from t1 as of scn 44545454;select * from t1 as of tim…

poj 2528 Mayor's posters(線段樹+離散化)

1 /*2 poj 2528 Mayors posters 3 線段樹 離散化4 5 離散化的理解:6 給你一系列的正整數, 例如 1, 4 , 100, 1000000000, 如果利用線段樹求解的話,很明顯7 會導致內存的耗盡。所以我們做一…

漢儀尚巍手書有版權嗎_為什么“漢儀尚巍手書”會大行天下?

昨夜,我寫了篇文章《莫選最丑尚巍體,要選美麗中國字!》發到朋友圈、微信群里,得到了一些朋友的反饋,有位朋友居然還認識尚巍,把他的微信推給了我。我加了尚巍的微信,待他通過后,便連…

如何查詢linux服務器的網卡,Linux服務器如何查看有沒有無線網卡

還是實驗室那臺服務器,連不上網。有沒有界面,所以想著如何用一些命令來鏈接上熱點。當然,在Linux下鏈接wifi沒有win下那么一點就好了!首先我們需要的基本條件就是: 服務器上有無線網卡。[roottomato2 ~]# iwconfiglo n…

java中如何生成可執行的jar文件

java中如何生成可執行的jar文件最簡單的方法就是:jar -cfe Card.jar CardLayoutDemo CardLayoutDemo$1.class CardLayoutDemo$myAct ionListener.class CardLayoutDemo.class myClosingListener.class myPanel.class jar命令為java自帶的專用打包工具; c…

ecs硬盤數據遷移_阿里云ECS新增數據盤以及遷移數據方法

第一、檢查數據占用以及數據盤我們從探針可以看到,本身有30GB的硬盤只用到不到10GB,而且系統和WDCP面板/網站都系統盤中。通過fdisk -l 我們可以看到還有21GB的沒有格式化和掛載,系統只用到10.7GB。第二、對數據盤分區fdisk /dev/xvdb第三、查…

linux文件瀏覽 ls,linux瀏覽文件命令

在linux下我們要瀏覽文件的內容,可以通過相關的命令來執行操作,下面由學習啦小編為大家整理了linux下瀏覽文件命令的相關知識,希望對大家有所幫助!linux瀏覽文件命令1.cat[功能說明]查看文件的內容#cat本身是一個串接命令,把指定一…

python的多行語句可以使用反斜杠_python 為什么不用分號作終止符?

作者:豌豆花下貓 來源:Python貓一般而言,編程語言中使用分號“;”來實現兩種目的:作為語句分隔符:使用分號來分隔語句(statement),這樣就能在一行代碼中書寫多條語句(一行…

linux dlopen 內存,Linux下加載庫的有關問題(dlopenm, dlsym)

Linux下加載庫的問題(dlopenm, dlsym)如題, 程序中發現load庫成功,但是加載函數的時候報錯: undefined symbol functionname是很簡單的一個東西,因為不熟悉,所以老是弄不好,請各位指導!代碼如下&#xff1a…

grafana zabbix 模板_Grafana + Zabbix 監控系統搭建

rafana:一個靜態項目,需要聯合nginx、apache等使用,友好的如下顯示首先安裝 grafana官網http://grafana.org/download/ 有好多版本可選,好幾種包形式,三種安裝方式(官方說明):1、yum直接安裝 rpm包&#xf…

java二維數組的常見初始化

public class Test{public static void main(String[] args){//第一種&#xff1a;//int[][] arr1 new int[][]{{1,2}, {2, 3}, {4, 5}};int[][] arr1 {{1,2}, {2, 3}, {4, 5}};System.out.println("arr1的數值&#xff1a;");for(int i0; i<3; i)for(int j0; j…

linux svn 備份腳本,SVN熱備份腳本

SVN熱備份腳本2011-08-03 徐磊#!/bin/sh########################################################## Script to do incremental rsync backups# modidfy: wanjie.info# date: 2010/06/04# 這個腳本不是xulei寫的&#xff0c;我只是拿來主義&#xff0c;當然如果大家看不明白…

python如何刪除對象屬性_如何優雅的刪除對象中的指定屬性?

要優雅的話&#xff0c;使用 Lodash 的 omit 方法移除不要的屬性&#xff1a;const object { a: 1, b: 2, c: 3 };const result _.omit(object, [a, c]);// > { b: 2 }或者用 pick 方法只留下需要的屬性&#xff1a;const object { a: 1, b: 2, c: 3 };const result _.p…

java接口的應用舉例

/* 接口的理解&#xff1a; 接口就是前期定義一個規則&#xff01;某一個類A&#xff0c;為了擴展自身的功能&#xff0c;對外提供這個接口&#xff0c;后期只要是符合這個接口&#xff08;規則&#xff09; 的類&#xff08;這個類是接口的子類&#xff09;&#xff0c;將子類…

linux 關閉scp服務器,Linux系統如何關閉scp和sftp命令

Linux系統如何關閉scp和sftp命令。sftp介紹sftp是Secure File Transfer Protocol的縮寫&#xff0c;安全文件傳送協議。可以為傳輸文件提供一種安全的加密方法。sftp 與 ftp 有著幾乎一樣的語法和功能scp介紹兩臺主機之間傳輸文件一般使用scp命令,通常用scp命令通過ssh獲取對方…

自動補足算法是什么_如何自定義Shell(Fish版)的自動補全規則?

默認fish能自動補全的命令已經相當多了,常見的apt-get&#xff0c;rpm等都沒問題&#xff0c;但今天卻發現沒有lsusb的補全規則,查看了下文檔&#xff0c;發現規則比bash-completion簡單不少&#xff0c;記錄下&#xff5e;簡單補全1. 建立自動補全規則文件默認自動補全路徑由全…