異常類型

”今天想再說一件事,在Java中,所有的異常都分為兩種:checkedunchecked(必須捕獲的和你不必捕獲的)。默認情況下,所有的異常都需要被捕捉。”

“你能故意在你的代碼中拋出異常嗎?”

“你可以在自己的代碼中拋出任何異常。你甚至可以編寫自己的異常。但我們稍後會討論這個。現在,讓我們專注於 Java 機器拋出的異常。”

“好的。”

“如果在方法中拋出(發生)ClassNotFoundExceptionFileNotFoundException ,開發人員必須在方法聲明中指明它們。這些是已檢查的異常。這通常是這樣的:”

檢查異常的例子
public static void method1() throws ClassNotFoundException, FileNotFoundException
public static void main() throws IOException
public static void main() // Doesn't throw any exceptions

“所以我們只寫‘throws’,然後是逗號分隔的異常列表,對吧?”

method1“是的。但還有更多。為了程序要編譯,在下面的例子中調用的方法必須做兩件事之一:要么捕獲這些異常,要么重新拋出它們(給調用者)在它的聲明中指明重新拋出的異常。

“再說一次。如果您的主要方法需要調用一個方法,其聲明包含‘ throws FileNotFoundException,……’,那麼您需要做以下兩件事之一:

1)趕上 FileNotFoundException,...

您必須將調用不安全方法的代碼包裝在try-catch塊中。

2)不要捕獲 FileNotFoundException,...

您必須將這些異常添加到方法的拋出列表中。”

“你能舉個例子嗎?”

“看看這個:”

檢查異常的例子
public static void main(String[] args)
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

“此示例中的代碼無法編譯,因為main方法調用method1(),它會拋出必須捕獲的異常。”

“要使其編譯,我們需要向 main 方法添加異常處理。您可以通過以下兩種方式之一執行此操作:”

選項 1:我們只是重新拋出異常(給調用者):
public static void main(String[] args)  throws FileNotFoundException, ClassNotFoundException
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

“在這裡我們用try-catch捕捉它:”

選項 2:捕獲異常:
public static void main(String[] args)
{
    try
    {
        method1();
    }
    catch(Exception e)
    {
    }
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

“它開始變得更清楚了。”

“看下面的例子。它會幫助你理解其餘的。”

我們不處理異常,而是將它們重新拋給知道如何處理它們的調用堆棧下方的方法:
public static void method2() throws FileNotFoundException, ClassNotFoundException
{
    method1();
}
處理一個異常並拋出另一個:
public static void method3() throws ClassNotFoundException
{
   try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
}
捕獲兩個異常,不拋出任何異常:
public static void method4()
{
    try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
    catch (ClassNotFoundException e)
    {
        System.out.println("ClassNotFoundException has been caught.");
    }
}
3
任務
Java Syntax,  等級 9課堂 4
上鎖
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).

“還有一種類型的異常, RuntimeException和繼承它的類。 你不需要捕獲它們。這些是未經檢查的異常。它們被認為很難預測。你可以用同樣的方式處理它們,但你不需要在throws子句中指明它們。”