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