Java -- Error 和 Exception 区别

今天突然看到java中exception与Error的区别,
突然有点忘记了,这里做了下记录,回忆整理下主要的知识点

下面图,来看下exception与Error的体系结构

  • Error: represents system errors occurred in abnormal conditions. It is the base class for all errors in Java. Remember that we should not catch errors, because the program cannot be recovered in the events of errors. You may know two well-known errors are StackOverflowError and OutOfMemoryError.
  • Exception: is the base class for all exceptions in Java. And most of the time, we throw and catch exceptions which are sub classes of Exception.
  • RuntimeException: an exception of this type represents a programming error and typically we should not throw and catch runtime exceptions. NullPointerException is a very well-know runtime exception in Java.

以上摘录内容来自:http://www.codejava.net/java-core/exception/java-exception-api-hierarchy-error-exception-and-runtimeexception

总结,通过上图,我们能耐很清晰的看出,Exception 与 Error 是属于throwable的子类。
Exception分为运行时异常和编译器异常。

  • RuntimeException:其特点是Java编译器不去检查它,也就是说,当程序中可能出现这类异常时,即使没有用try……catch捕获,也没有用throws抛出,还是会编译通过,如除数为零的ArithmeticException、错误的类型转换、数组越界访问和试图访问空指针等。处理RuntimeException的原则是:如果出现RuntimeException,那么一定是程序员的错误。

  • 受检查的异常(IOException等):这类异常如果没有try……catch也没有throws抛出,编译是通不过的。这类异常一般是外部错误,例如文件找不到、试图从文件尾后读取数据等,这并不是程序本身的错误,而是在应用环境中出现的外部错误。
    参考:https://blog.csdn.net/goodlixueyong/article/details/47122487

staticOverflow上的回答:https://stackoverflow.com/questions/912334/differences-between-exception-and-error

Error -

  1. Java中的错误类型为java.lang.Error。
  2. Java中的所有Error都是未经检查的类型。
  3. 运行时发生错误。 编译器不会知道它们。
  4. 从Error中恢复是不可能的。
  5. Error主要是由运行应用程序的环境引起的。
  6. 示例:java.lang.StackOverflowError,java.lang.OutOfMemoryError

Exception -

  1. java中的Exception是java.lang.Exception类型。
  2. 异常包括检查和未检查类型。
  3. 编译器已知检查异常,因为编译器不知道未检查的异常,因为它们在运行时发生。
  4. 您可以通过try-catch块处理异常来恢复异常。
  5. 异常主要是由应用程序本身引起的。
  6. 示例:检查异常:SQLException,IOException
  7. 未经检查的异常:ArrayIndexOutOfBoundException,ClassCastException,NullPointerException
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值