您的位置:首页 > 编程语言 > Java开发

Java Checked 和 UnChecked Exception 的区别

2014-03-18 05:39 309 查看
这个问题已经在不仅一次的面试中出现了,因此收录到博客里!

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: