人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

Java編程思想讀書筆記(10章中)

Java編程思想讀書筆記(10章中)

更新時間:2022-07-26 文章作者:未知 信息來源:網絡 閱讀次數:

三. 以finally進行清理

  1. 如果某段代碼不管是否發生異常都要執行,那可把它改入finally塊中。

   import java.sql.SQLException;
   class TestException{
   public static void tSql() throws SQLException {
   System.out.println("Originating the exception in tSql()");
   throw new SQLException("throw in tSql");
   }
   public void f() throws SQLException{
   try{
   tSql();
   }
   catch(SQLException ex){
   System.out.println("catch SQLException in f()");
   throw ex;//(1)

   }
   finally{
   System.out.println("finally in f()");
   }
   }
   }
   public class Test{
   public static void main(String[] args){
   TestException te = new TestException();
   try{
   te.f();
   }
   catch(SQLException ex){
   System.out.println("catch te.f() SQLException in main");
   }
   catch(Exception ex){
   System.out.println("catch te.f() Exception in main");
   }
   }
   }
   運行結果為:

   Originating the exception in tSql()

   catch SQLException in f()

   finally in f()

   catch te.f() SQLException in main
   雖然在代碼(1)處重新拋出異常,但finally塊中的代碼仍然會被執行。

   2. finally造成的異常遺失

   如果在finally中執行的代碼又產生異常,那么在上一層調用中所捕捉到的異常的起始拋出點會是finally所在的函數。

   import java.sql.SQLException;
   class TestException{
   public static void tSql1() throws SQLException {
   System.out.println("Originating the exception in tSql()");
   throw new SQLException("throw in tSql1");
   }
   public static void tSql2() throws SQLException {
   System.out.println("Originating the exception in tSql()");
   throw new SQLException("throw in tSql2");
   }
   public void f() throws SQLException{
   try{
   tSql1();
   }
   catch(SQLException ex){
   System.out.println("catch SQLException in f()");
   throw ex;//(2)

   }
   finally{
   System.out.println("finally in f()");
   //tSql2();(1)

   }
   }
   }
   public class Test{
   public static void main(String[] args){
   TestException te = new TestException();
   try{
   te.f();
   }
   catch(SQLException ex){
   System.out.println("catch te.f() SQLException in main");
   System.out.println("getMessage:" + ex.getMessage());
   System.out.println("printStackTrace:");
   ex.printStackTrace();
   }
   }
   }
   運行結果為:

   Originating the exception in tSql()

   catch SQLException in f()

   finally in f()

   catch te.f() SQLException in main
   getMessage:throw in tSql1
   printStackTrace:

   java.sql.SQLException: throw in tSql1
   void TestException.tSql1()

   Test.java:5
   void TestException.f()

   Test.java:13
   void Test.main(java.lang.String[])

   Test.java:29
   從結果可以看出,在main()中能正確打印出所捕捉到的異常的起始拋出點。但如果去掉代碼(1)的注釋,結果將變為:

   Originating the exception in tSql()

   catch SQLException in f()

   finally in f()

   Originating the exception in tSql()

   catch te.f() SQLException in main
   getMessage:throw in tSql2
   printStackTrace:

   java.sql.SQLException: throw in tSql2
   void TestException.tSql2()

   Test.java:9
   void TestException.f()

   Test.java:21
   void Test.main(java.lang.String[])

   Test.java:29
   從結果可以看出,在main()中捕捉到的異常是finally中產生的異常,代碼(2)中拋出的異常丟失了。
  
  
  

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 依兰县| 盘锦市| 宝山区| 江城| 枣阳市| 大田县| 昌图县| 肃南| 山丹县| 齐齐哈尔市| 舟曲县| 万源市| 呼玛县| 莱芜市| 开江县| 宿松县| 霍山县| 开阳县| 革吉县| 驻马店市| 泗洪县| 和龙市| 阿瓦提县| 会东县| 鹿泉市| 雷州市| 新和县| 沙洋县| 临沂市| 托里县| 衡阳县| 突泉县| 桂平市| 无棣县| 阳原县| 疏勒县| 曲阳县| 长海县| 丰镇市| 汾西县| 仁寿县|