这个问题已经在不仅一次的面试中出现了,因此收录到博客里!
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中的异常处理机制,包括Checked和UnChecked异常的区别。通过具体的代码示例,展示了这两种异常如何在程序中体现,并解释了它们在编译期和运行期的不同表现。
637

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



