免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
try/catch/finally測試

try/catch/finally測試  

結論:
1、如果某個異常發(fā)生的時候沒有在任何地方捕獲它,程序就會終止執(zhí)行,并在控制臺上顯示異常信息
2、不管是否有異常被捕獲,finally子句中的代碼都會執(zhí)行
3、當代碼拋出一個異常時,就會終止對方法中剩余代碼的處理,并退出這個方法
4、finally后面的代碼被執(zhí)行的條件:
    a、代碼沒有拋出異常
    或者b、代碼拋出異常被捕獲,而且捕獲的時候沒有再拋出異常
5、建議用try/catch和try/finally語句塊,前者是將異常攔截;而后者是為了將異常送出去,并執(zhí)行所需代碼
6、finally優(yōu)先于try中的return語句執(zhí)行,所以finally中,如果有return,則會直接返回,而不是調用try中的return;catch中的return也是一樣,也就是說,在return之前,肯定會先調用finally中的return

例1:
// 測試捕獲異常后,再次拋出異常,不會執(zhí)行finally后面的語句// 1
// 因為編譯就通不過,直接報:unreacchable statement
private static String testTryCatch() throws Exception
{
  try
  {
    throw new Exception();
  }
  catch (Exception e)
  {
    // 捕獲異常后再次拋出異常    
    throw new Exception();
  }
  finally
  {
    System.out.println("finally");  
  }
  return "after finally";  // 1
}

例2:
// 測試異常被捕獲,會繼續(xù)執(zhí)行finally后面的語句// 1
private static void testTryCatch()
{
  try
  {
    throw new Exception();
  }
  catch (Exception e)
  {
    e.printStackTrace();
    System.out.println("exception");
  }
  finally
  {
    System.out.println("finally");  
  }
  System.out.println("after finally");    // 1
}
// 輸出:
java.lang.Exception
    at Main.testTryCatch(Main.java:39)
    at Main.main(Main.java:10)
exception
finally
after finally

例3:
// 測試finally中的return優(yōu)先級最高
private static String testTryCatch()
{
  try
  {
    throw new Exception();
  }
  catch (Exception e)
  {
    e.printStackTrace();
    return "exception";    // 2
  }
  finally
  {
    return "finally";        // 1    
  }
}
// 輸出
最后返回的結果是// 1中的"finally"

例4:
public static void main(String[] args)
    {
        Test test = new Test();
        try
        {
            test.testTryCatch();
        }
        catch (Exception e)
        {
            System.out.println("exception");    // 3
        }
    }
   
    // 測試try/finally語句塊拋出異常的執(zhí)行順序
    private void testTryCatch() throws Exception
    {
      try
      {
          System.out.println("try");        // 1
          throw new Exception();
      }
      finally
      {
        System.out.println("finally");      // 2   
      }
    }

// 輸出:
try
finally
exception
也就是說,先執(zhí)行try(// 1),然后執(zhí)行finally(// 2),然后執(zhí)行拋出的異常(// 3)

例5:
public static void main(String[] args)
    {
        Test test = new Test();
        try
        {
            test.testTryCatch();
        }
        catch (IOException e)
        {
            System.out.println("exception"); // 4
        }
    }
   
    // 測試catch中又拋出異常,其執(zhí)行順序
    private void testTryCatch() throws IOException
    {
      try
      {
          System.out.println("try");        // 1
          throw new Exception();
      }
      catch (Exception e)
      {
          System.out.println("catch before throw new exception");    // 2
          throw new IOException();
//          System.out.println("catch after throw IOException"); // 無法到達的代碼
      }
      finally
      {
        System.out.println("finally");      // 3
      }
    }

輸出:
try
catch before throw new exception
finally
exception
也就是說,先執(zhí)行try(//1),再執(zhí)行catch(//2),然后finally(//3),最后外部的catch(//4)。
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java try、catch、finally及finally執(zhí)行順序詳解
異常
Java 中的異常和處理詳解
try-catch和throw,throws的區(qū)別和聯系
Java異常發(fā)生時程序的執(zhí)行順序
java中異常概念及分類、捕獲異常、聲明異常
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服