这个问题已经在不仅一次的面试中出现了,因此收录到博客里!
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/
本文深入探讨了Java中的检查异常与未检查异常的概念,并通过具体实例展示了如何在编译时和运行时捕获这些异常。文章还提供了一张详细的异常继承关系图,帮助读者理解不同异常类之间的关系。
637

被折叠的 条评论
为什么被折叠?



