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;
}
}
再来一张继承关系图:
http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/
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 interface 用法
- 《Java编程周末提高班(第一期)》第2天见闻 2014.03.16
- 近似求PI
- 第三周实验
- 建立简单的SpringMVC框架
- 【实现】实现随机点名的签到程序
- 140219 java的一些常见运算符和简单IO
- Java学习路线图,专为新手定制的Java学习计划建议
- 第二周实验:Java事件处理
- 实现类似spring的可配置的AOP框架
- 140217 java第一课
- java 软引用 弱引用 缓存
- JAVA深复制(深克隆)与浅复制(浅克隆)
- 深入理解Java虚拟机到底是什么
- Java 编程的动态性,第 1 部分: 类和类装入
- 14、java打开文件
- 13、java中几种不同文件路径的差别(绝对路径,全路径)
- 12、java复制文件的四种方法
- 11、java读取文件的几种方式
- Java发展史之Java由来