详解Java中的checked异常和unchecked异常区别
(⼀)Java的异常层次结构
要想明⽩Java中checked Exception和unchecked Exception的区别,我们⾸先来看⼀下Java的异常层次结构。
这是⼀个简化的Java异常层次结构⽰意图,需要注意的是所有的类都是从Throwable继承⽽来,下⼀层则分为两个结构,Error和Exception。其中Error类层次描述了Java运⾏时系统的内部错误和资源耗尽错误,这种错误除了简单的报告给⽤户,并尽⼒阻⽌程序安全终⽌之外,⼀般也⽶有别的解决办法了。
(⼆)unchecked异常和checked异常的区别
有了上⾯的认识之后,我们再来看什么是checked异常,什么是unchecked的异常。其实,Java语⾔规范对这两个定义⼗分简单,将派⽣于Error或者RuntimeException的异常称为unchecked异常,所有其他的异常成为checked异常。尽管,这个定义⼗分简单,但是RuntimeException却是⼀个⾮常让⼈容易混淆的观念,似乎我们所有的异常都是在程序运⾏的过程中。我《Effective Java》中关于Ru ntimeException异常的阐述也不是那么尽如⼈意,
Use checked exceptions for recoverable conditions and runtime exceptions for programming errors (Item 58 in 2nd edition)
不过从这句话中我们可以简单引申⼀下,也就是说,如果出现了RuntimeException,就⼀定是程序员⾃⾝的问题。⽐如说,数组下标越界和访问空指针异常等等,只要你稍加留⼼这些异常都是在编码阶段可以避免的异常。如果你还是觉得这两个概念不好区分,那么“最暴⼒“的⽅法就是将常见的RuntimeException背下来,这样就可以省去很多判断的时间。
(三)为什么要对unchecked异常和checked异常进⾏区分?
原因其实很简单,编译器将检查你是否为所有的已检查异常提供了异常处理机制,⽐如说我们使⽤Class.forName()来查给定的字符串的class对象的时候,如果没有为这个⽅法提供异常处理,编译是⽆法通过的。
(四)我们应该对哪些异常进⾏声明?
我们前⾯说,RuntimeException是在programing过程中可以避免的错误,那是不是我们就不需要抛出这些异常呢?原则上来说,是这样的,但是Java规范中并没有对此进⾏限制,只是看上去你抛出⼀个数组越界的异常并没有多少实际意义,相反还会对性能造成⼀定的损失。那么我们应该如何来设计抛出异常呢?我们要记住以下两种情况是必声明throws异常的:
调⽤⼀个checked异常的⽅法,例如IOException,⾄于原因我们前⾯已经讨论过了,如果抛出所有的ch
ecked异常时⽆法通过编译的。程序运⾏过程中发现错误,利⽤throw语句抛出⼀个异常。对于unchecked异常,⽆⾮主要是两种情况要么是可以避免的(Runtime Exception),要么是不可控制的。这些也是需要声明异常的。
下⾯列举例⼦说明上⾯的注意事项2中提到的⽐较别扭的地⽅:
⾸先定义⼀个基本的异常类GenericException,继承于Exception。
package check_unchecked_exceptions;
public class GenericException extends Exception{
/**
*
*/
private static final long serialVersionUID = 2778045265121433720L;
public GenericException(){
}
public GenericException(String msg){
super(msg);
}
}
下⾯定义⼀个测试类VerifyException。
package check_unchecked_exceptions;
public class VerifyException {
public void first() throws GenericException {
throw new GenericException("checked exception");
}
public void second(String msg){
if(msg == null){
throw new NullPointerException("unchecked exception");
}
}
public void third() throws GenericException{
first();
}
public static void main(String[] args) {
nullpointerexception为什么异常VerifyException ve = new VerifyException();
try {
ve.first();
} catch (GenericException e) {
e.printStackTrace();
}
ve.second(null);
}
}
运⾏后,在eclipse的控制台上得到下⾯的信息:
check_unchecked_exceptions.GenericException: checked exception
at check_unchecked_exceptions.VerifyException.first(VerifyException.java:6)
at check_unchecked_exceptions.VerifyException.main(VerifyException.java:23)
Exception in thread "main" java.lang.NullPointerException: unchecked exception
at check_unchecked_exceptions.VerifyException.second(VerifyException.java:11)
at check_unchecked_exceptions.VerifyException.main(VerifyException.java:29)
上⾯的例⼦,结合checked以及unchecked的概念,可以看出Exception这个⽗类是checked类型,但是其⼦类RuntimeException (⼦类NullPointerException)却是unchecked的。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。