六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

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

[摘要]三. 以finally進行清理  1. 如果某段代碼不管是否發生異常都要執行,那可把它改入finally塊中。   import java.sql.SQLException;   class TestException   public static void tSq...
三. 以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)中拋出的異常丟失了。
  
  
  



主站蜘蛛池模板: 天天干天天操天天碰 | 日本亚洲免费 | 亚洲综合色丁香婷婷六月图片 | 欧美亚洲国产专区在线app | 欧美性xxxx极品高清3d | 色五月激情五月 | 五月婷婷操 | 五月婷婷丁香在线观看 | 婷婷久久综合九色综合九七 | 午夜精品久久久久久久99热下载 | 一级女人毛片人一女人 | 在线观看欧美亚洲 | 日本三级理论片 | 日韩美女在线视频网站免费观看 | 日本午夜www高清视频 | 网红福利在线 | 综合久久久久综合体桃花网 | 日本免费高清在线观看播放 | 色碰视频 | 四虎国产精品永久地址48 | 日日摸日日添夜夜爽97 | 亚洲国产成人久久综合碰 | 欧美综合精品一区二区三区 | 亚洲精品有码在线观看 | 亚洲人与牲动交xxxxbbbb | 亚洲国产欧美精品 | 欧美一区亚洲 | 午夜黄大色黄大片美女图片 | 四虎影视在线免费观看 | 三级黄色片在线观看 | 日韩 欧美 国产 亚洲 制服 | 日本一本二本视频 | 亚洲一区www| 亚洲精品在线第一页 | 天天做天天躁天天躁 | 日韩有码在线观看 | 青青草国产在线视频 | 四虎家庭影院 | 宇都宫紫苑日韩专区亚洲 | 色屁屁影院免费观看入口 | 天天干天天操天天干 |