📜  Java中的已检查异常与未检查异常

📅  最后修改于: 2022-05-13 01:54:45.062000             🧑  作者: Mango

Java中的已检查异常与未检查异常

异常是在程序执行期间(即在运行时)发生的不希望的或意外事件,它破坏了程序指令的正常流程。在Java中,有两种类型的异常:

  1. 已检查的异常
  2. 未经检查的异常

检查异常

这些 是在编译时检查的异常。如果方法中的某些代码引发了已检查异常,则该方法必须要么处理异常,要么必须使用throws关键字指定异常。

例如,考虑以下Java程序,它打开位于“C:\test\a.txt”位置的文件并打印它的前三行。该程序无法编译,因为函数main() 使用 FileReader() 并且 FileReader() 抛出检查异常FileNotFoundException 。它还使用了 readLine() 和 close() 方法,这些方法也会抛出检查异常IOException

例子:

Java
// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException occured
  
// Importing I/O classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Reading file from path in local directory
        FileReader file = new FileReader("C:\\test\\a.txt");
  
        // Creating object as one of ways of taking input
        BufferedReader fileInput = new BufferedReader(file);
  
        // Printing first 3 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 3; counter++)
            System.out.println(fileInput.readLine());
  
        // Closing file connections
        // using close() method
        fileInput.close();
    }
}


Java
// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException does not occur
  
// Importing I/O classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Creating a file and reading from local repository
        FileReader file = new FileReader("C:\\test\\a.txt");
  
        // Reading content inside a file
        BufferedReader fileInput = new BufferedReader(file);
  
        // Printing first 3 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 3; counter++)
            System.out.println(fileInput.readLine());
  
        // Closing all file connections
        // using close() method
        // Good practice to avoid any memory leakage
        fileInput.close();
    }
}


Java
// Java Program to Illustrate Un-checked Exceptions
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Here we are dividing by 0
        // which will not be caught at compile time
        // as there is no mistake but caught at runtime
        // because it is mathematically incorrect
        int x = 0;
        int y = 10;
        int z = y / x;
    }
}


输出:

要修复上述程序,我们要么需要使用 throws 指定异常列表,要么需要使用 try-catch 块。我们在下面的程序中使用了 throws。由于FileNotFoundExceptionIOException的子类,我们只需在 throws 列表中指定IOException即可使上述程序无编译错误。

例子:

Java

// Java Program to Illustrate Checked Exceptions
// Where FileNotFoundException does not occur
  
// Importing I/O classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
  
        // Creating a file and reading from local repository
        FileReader file = new FileReader("C:\\test\\a.txt");
  
        // Reading content inside a file
        BufferedReader fileInput = new BufferedReader(file);
  
        // Printing first 3 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 3; counter++)
            System.out.println(fileInput.readLine());
  
        // Closing all file connections
        // using close() method
        // Good practice to avoid any memory leakage
        fileInput.close();
    }
}

输出:

First three lines of file "C:\test\a.txt"

未经检查的异常

这些是在编译时未检查的异常。在 C++ 中,所有异常都是未经检查的,因此编译器不会强制它处理或指定异常。程序员要文明,并指定或捕获异常。在Java中, ErrorRuntimeException类下的异常是未经检查的异常,throwable 下的所有其他内容都经过检查。

考虑以下Java程序。它编译得很好,但是在运行时会抛出ArithmeticException 。编译器允许它编译,因为ArithmeticException是未经检查的异常。

例子:

Java

// Java Program to Illustrate Un-checked Exceptions
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Here we are dividing by 0
        // which will not be caught at compile time
        // as there is no mistake but caught at runtime
        // because it is mathematically incorrect
        int x = 0;
        int y = 10;
        int z = y / x;
    }
}

输出:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Main.main(Main.java:5)
Java Result: 1