簡體   English   中英

理解JAVA中的throw關鍵字

[英]understanding throw keyword in JAVA

在JAVA中進行異常處理練習時,我對各種事情感到困惑。 基本上我不明白的是當遇到異常時程序的流程如何。 我想了解程序的流程實際上是如何在以下場景中進行的,以及我對這些概念的理解是對還是錯。

 public void myFunction(){

     try{

            //Some code......

        }catch(Exception e1){

            //If this Exception is occured handle it here.

        }catch(Exception e2){

           //if this exception has occured then

          throw new myException("whatever message required");
        }

       finally{

           //code that has to be executed 
       }

 }

現在我的理解是:

1.如果沒有異常發生,則代碼運行平穩,最終執行finally塊中的代碼2.如果發生異常e1,則將其捕獲到第一個catch塊中,在那里進行適當處理,然后最終執行塊。 但是如果發生異常e2會發生什么。 在那個catch塊中我們拋出一個新的異常。 所以我調用myFunction的方法應該提供一些機制來處理這個myException? 所以執行將轉移到調用方法的catch塊。對嗎? 那么myFunction()的“finally”塊會發生什么? 它不會被執行呢? 如何進行程序流程? 我真的很難發現當我們使用“throw”時會發生什么。 我們使用它時會發生什么?

請參閱(您的修改示例):

try {
  // Some code ...
}
catch(SomeException e1) {
  // SomeException code ...
}
catch(SomeOtherException e2) { // SomeOtherException is NOT SomeException extension
  throw new myException("whatever message required");
}
finally {
  // Some final code ...
}

執行可能性:

 1. No exception at all:
   Some code executed
   Some final code executed
 2. SomeException is thrown:
   Some code (may be partially) executed  
   SomeException code executed
   Some final code executed
 3. SomeOtherException is thrown:
   Some code (may be partially) executed
   Some final code executed
   throw new myException("whatever message required");
 4. SomeStrangeException is thrown 
   Some code (may be partially) executed 
   Some final code executed
   System'll look for other try {} catch {} block to catch SomeStrangeException 

考慮下面的例子(Basicalliy你的例子只是填充了一些真正的代碼,它被設計為拋出NullPointerException):

public class Exceptions {

    public static void myMethod() {
        try{
            String s = null;
            s.length();
        }
        catch(NumberFormatException e1){
            System.out.println("Something unexpected happend");
        }
        catch(NullPointerException e2){
            throw new RuntimeException("Exactly what we want happened");
        }
        finally{
            System.out.println("Finally Block");
        }
    }

    public static void main(String[] args){
        try{
            myMethod();
        }
        catch(RuntimeException e){
            e.printStackTrace();
        }
    }
}

其輸出是:

Finally Block
java.lang.RuntimeException: Exactly what we want happened
    at Exceptions.myMethod(Exceptions.java:14)
    at Exceptions.main(Exceptions.java:23)

所以你可以看到,在將新的RuntimeException傳遞給調用方法之前執行finally塊。 否則,“Finally Block”輸出將在RuntimeException的堆棧跟蹤之后。

“我調用myFunction的方法應該提供一些機制來處理這個myException?” 它應該,但只有當myException擴展了已檢查的異常時,編譯器才會讓你這樣做。 無論發生什么,最終塊將始終執行。

您可以在Java語言規范第11.3節中找到有關如何在運行時處理Java異常的所有知識。

本質上,當遇到異常時,程序流停止並且控制向上傳遞到調用堆棧,直到異常被try/catch或直到異常到達堆棧的底部,此時線程終止。

所以換句話說,“發生了一些事情,停止你正在做的事情,轉到最接近匹配此異常的catch塊,除非不存在catch塊,在這種情況下殺死線程”

在您的情況下, e2將永遠不會發生,因為所有異常都已被第一個catchcatch 如果要以不同的方式處理不同的異常,通常會使用多個catch塊,例如,如果要從文件中解析數字,則在一個塊中捕獲IOException在另一個塊中捕獲NumberFormatException

您不必處理運行時異常(擴展RuntimeException異常)。 您必須顯式處理已檢查的異常 (擴展Exception但不是RuntimeException Exception )。

無論是否拋出異常, finally塊中的代碼都會被執行。

這將有助於您更好地理解catch塊

http://www.c4learn.com/java/java-multiple-catch-blocks/

此外,在java 7中,支持多異常捕獲:

try {
 // stuff
} catch (Exception1 | Exception2 ex) {
 // Handle both exceptions
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM