簡體   English   中英

異常處理; 試着抓

[英]Exception Handling; Try Catch

這是我的代碼:

class FinallyDemo {
    static void myMethod(int n) throws Exception{
        try {
            switch(n) {
                case 1: 
                    System.out.println("1st case");
                    return;
                case 3: 
                    System.out.println("3rd case");
                    throw new RuntimeException("3!");
                case 4: 
                    System.out.println("4th case");
                    throw new Exception("4!");
                case 2: 
                    System.out.println("2nd case");
            }
        catch (RuntimeException e) {
            System.out.print("RuntimeException: ");
            System.out.println(e.getMessage());
        } finally {
            System.out.println("try-block entered.");
        }
    }

    public static void main(String args[]){
        for (int i=1; i<=4; i++) {
            try {
                FinallyDemo.myMethod(i);
            } catch (Exception e){
                System.out.print("Exception caught: ");
                System.out.println(e.getMessage());
            }
            System.out.println();
        }
    }
}

現在,它不是這樣工作的:

如果我在方法本身中有一個try and catch塊,那么我不需要寫

method_name(int n) throws Exception

引發異常的方法中的try-catch塊是否可以防止在引發異常的方法中寫入“引發異常”?

在您的示例中,案例4引發了一個異常,而在catch中,您只是在捕獲RuntimeException。 由於沒有捕獲到Exception,因此您的方法需要聲明它拋出Exception。 如果要為Exception添加一個捕獲,則不需要拋出Exception。 這將起作用。

static void myMethod(int n) {
    try {
        switch (n) {
            case 1:
                System.out.println("1st case");
                return;
            case 3:
                System.out.println("3rd case");
                throw new RuntimeException("3!");
            case 4:
                System.out.println("4th case");
                throw new Exception("4!");
            case 2:
                System.out.println("2nd case");
        }
    } catch (RuntimeException e) {
        System.out.print("RuntimeException: ");
        System.out.println(e.getMessage());
    } catch (Exception e) {
        System.out.print("Exception: ");
        System.out.println(e.getMessage());
    } 
    finally {
        System.out.println("try-block entered.");
    }
}

當且僅當捕獲了所拋出的異常類型(或它擴展了RuntimeException時,才不需要throws子句。在您的情況下,您使用語句throw new Exception("4!");拋出了Exception ,但是您只捕獲類型RuntimeException

如果為Exception添加catch塊,則不再需要throws子句。 例如:

static void myMethod(int n) throws Exception{
    try {
        switch(n) {
        case 1: 
            System.out.println("1st case");
            return;
        case 3: 
            System.out.println("3rd case");
            throw new RuntimeException("3!");
        case 4: 
            System.out.println("4th case");
            throw new Exception("4!");
        case 2: 
            System.out.println("2nd case");
        }
    } catch (RuntimeException e) {
        System.out.print("RuntimeException: ");
        System.out.println(e.getMessage());
    } catch(Exception e) {
        System.out.print("Exception: ");
        System.out.println(e.getMessage());
    } finally {
        System.out.println("try-block entered.");
    }
}

是的,前提是您要捕獲該方法可能引發的所有異常類型。

在您的代碼中,您拋出Exception但不為其提供catch塊(您僅捕獲RuntimeException ),因此必須將您的方法聲明為throwing Exception

您將需要:

 ...
 catch (RuntimeException e) {
     System.out.print("RuntimeException: ");
     System.out.println(e.getMessage());
 } catch (Exception e) {
     System.out.print("Exception: ");
     System.out.println(e.getMessage());
 } finally {
 ...

引發異常的方法中的try-catch塊是否可以防止在引發異常的方法中寫入“引發異常”?

不,您始終可以聲明引發異常,即使您不這樣做也是如此。

除其他外,這對允許子類拋出它們很有用(因為不允許子類添加其他throw子句)。 它還允許您以后更改實現而不更改異常接口。

現在是兩種例外

  1. Exception的子類。
  2. RuntimeException子句

Exception的子類被稱為檢查異常,編譯器確保通過try / catch塊或通過修飾符在方法上拋出Exception (或子類)來對它們進行管理。

RuntimeException的子類被稱為未經檢查的異常,並且編譯不需要任何機制來對其進行管理。

現在,如果在方法上使用修飾符throws Exception (或子類),則編譯器將要求您使用try / catch對其進行管理。

由於在開關中同時拋出RuntimeExceptionException ,因此您要么需要捕獲兩者,要么方法需要拋出Exception以便可以在調用myMethod的方法中對其進行處理。

要同時使用:

catch (RuntimeException e) {
        System.out.print("RuntimeException: ");
        System.out.println(e.getMessage());
}catch (Exception e) {
        System.out.print("Exception: ");
        System.out.println(e.getMessage());
}

確保Exception的捕獲始終是最后一個,否則它將捕獲RuntimeException因為它擴展了Exception

您正在以兩種方式處理異常。 首先,如果像在聲明方法時所做的那樣擴展直接調用Exception類,

method_name(int n) throws Exception

這意味着無論方法中發生什么類型的異常,它都將始終能夠捕獲它,例如,如果方法內部發生算術異常或NullPointerException或ArrayIndexOutOfBoundsException,則上述聲明將能夠捕獲每個異常,並且每個人。 因此,將try catch塊放置在該方法中並沒有實際目的,因為即使RunTimeException也是Exception Class層次結構的一部分。 因此,如果我正確理解了您的問題,程序將執行,然后從catch塊中捕獲異常,否則,它將從方法聲明中捕獲該異常。 希望它能回答您的查詢。

暫無
暫無

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

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