Java中的已检查异常和未检查异常之间有何区别?

受检查的异常是在编译时发生的异常,这些也称为编译时异常。这些异常不能在编译时简单地忽略。程序员应注意(处理)这些异常。

示例

如果在程序中使用FileReader类从文件中读取数据,如果在其构造函数中指定的文件不存在,则会发生FileNotFoundException,并且编译器会提示程序员处理该异常。

import java.io.File;
import java.io.FileReader;

public class FilenotFound_Demo {
   public static void main(String args[]) {
      File file = new File("E://file.txt");
      FileReader fr = new FileReader(file);
   }
}

输出结果

C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported exception
FileNotFoundException; must be caught or declared to be thrown
    FileReader fr = new FileReader(file);
                    ^
1 error