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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP