Java -- Error 和 Exception 区别 - Go语言中文社区

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
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u010926176/article/details/80800972
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-20 11:54:01
  • 阅读 ( 592 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢