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

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

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)中拋出的異常丟失了。
  
  
  



主站蜘蛛池模板: 亚洲免费网址 | 日韩视频在线精品视频免费观看 | 色综合久久久久久中文网 | 亚洲视频在线免费播放 | 五月婷六月 | 午夜影院欧美 | 亚洲福利国产 | 色妹子影院 | 日韩视频欧美视频 | 午夜在线观看视频在线播放版 | 亚洲福利视频网址 | 伊人小视频 | 婷婷伊人五月 | 婷婷深爱网 | 欧美午夜一区二区福利视频 | 亚洲欧美日韩国产 | 欧洲成人在线 | 偷窥自拍第1页 | 日韩大片免费观看视频播放 | 色迷迷网免费站视频在线观看 | 亚洲天天综合网 | 亚洲精品综合 | 色女人天堂| 日韩美一区二区 | 日韩精品视频在线观看免费 | 亚洲精品欧洲精品 | 日韩av东京社区男人的天堂 | 日日夜夜免费精品 | 日本a网站| 最好韩国日本高清 | 一二三四视频免费观看影视 | 日本欧美高清全视频 | 欧美一区二区三区日韩免费播 | 欧美一级特黄乱妇高清视频 | 日韩性视频网站 | 天天做天天做天天综合网 | 伊人网在线免费观看 | 亚洲综合15p | 日韩精品亚洲专区在线影视 | 又粗又硬又爽的三级视频在线观看 | 夜夜快播|