这个问题已经在不仅一次的面试中出现了,因此收录到博客里!
Checked: 能在compile时被检查到
比如:
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("C:\\test\\a.txt");
BufferedReader fileInput = new BufferedReader(file);
// Print first 3 lines of file "C:\test\a.txt"
for (int counter = 0; counter < 3; counter++)
System.out.println(fileInput.readLine());
fileInput.close();
}
}
UnChecked:只能在runtime(运行时)才能被检查到的。
如除以0
class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}
再来一张继承关系图:
+-----------+ | Throwable | +-----------+ / \ / \ +-------+ +-----------+ | Error | | Exception | +-------+ +-----------+ / | \ / | \ \________/ \______/ \ +------------------+ unchecked checked | RuntimeException | +------------------+ / | | \ \_________________/ unchecked
http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/