java 的Exception和Error

Exception和Error都是Throwable的子类,下面是javase文档关于error的介绍

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

error是一般应用都不该去捕获的,这是严重的错误。

Exception是异常,是正常的系统也有可能出现的意料之外的情况。

Exception分为检查异常和非检查异常,检查异常要求在编写程序的时候进行捕获,非检查型异常并不需要。非检查型异常只有RuntimeException和它的子类。其他都是检查型异常。RuntimeException异常可以编程人员可以避免的,比如NullPointException和ArrayIndexOutOfBoundException,ClassCastException,对于空指针,程序员可以使用之前进行判断而避免异常;对于数组越界,可以进行判断数组是否越界而避免异常;检查型异常主要有IOException,ReflectiveOperationException及他们的子类,比如常见的FileNotFoundException。这类异常是编程人员无法通过改善程序避免的,系统希望应用捕获并且从异常中恢复的。

猜你喜欢

转载自blog.csdn.net/u012413167/article/details/51322925
今日推荐