java异常体系

java异常体系
​ 上述是java异常体系的类图, 主要包含Throwable,Error,Exception, RuntimeException, CheckException几个方面。

1、Throwable类

官方解释:

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

Throwable 类是 Java 语言中所有错误和异常的超类。 只有属于此类(或其子类之一)的实例的对象才会被 Java 虚拟机抛出或可以被 Java throw 语句抛出。 同样,只有此类或其子类之一可以是 catch 子句中的参数类型。

2、 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 是 Throwable 的一个子类,它指示合理的应用程序不应尝试捕获的严重问题。 大多数此类错误是异常情况。 ThreadDeath 错误虽然是“正常”情况,但也是 Error 的一个子类,因为大多数应用程序不应该尝试捕获它。
一个方法不需要在它的 throws 子句中声明在方法执行期间可能抛出但没有被捕获的 Error 的任何子类,因为这些错误是不应该发生的异常情况。 也就是说,出于编译时检查异常的目的,Error 及其子类被视为未经检查的异常。

3、 Exception 类

官方解释:

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor’s throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary

Exception 类及其子类是 Throwable 的一种形式,它指示合理的应用程序可能想要捕获的条件。
Exception 类和任何不是 RuntimeException 子类的子类都是检查异常。 如果检查异常可以通过方法或构造函数的执行抛出并传播到方法或构造函数边界之外,则需要在方法或构造函数的 throws 子句中声明它们

4、 RuntimeException类

官方解释:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor’s throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

RuntimeException 是那些可以在 Java 虚拟机正常运行期间抛出的异常的超类。
RuntimeException 及其子类是未经检查的异常。 未经检查的异常不需要在方法或构造函数的 throws 子句中声明,如果它们可以通过方法或构造函数的执行抛出并传播到方法或构造函数边界之外。

5、CheckException类

CheckException是 Exception的非(RuntimeException类及其子类)的子类的统称。

作用:

​ 1、 常用与编译时的异常检查

总结

  • Throwable: Throwable 类是 Java 语言中所有错误和异常的超类
  • Error: 它指示应用程序不应尝试捕获的严重问题
  • Exception: 它指示应用程序应尝试捕获的条件, 运行时异常,检查异常的超类,
  • RuntimeException(运行时异常): 程序运行时的异常,不需要处理
  • CheckException(检查异常、编译时异常): 程序编译时的异常,需要手动处理,处理方式为(1、 throws 声明 2、显式try—catch捕获)
上一篇:卧槽!Log4j2 再爆雷,Log4j v2.17.0 横空出世。。。


下一篇:org.thymeleaf.exceptions.TemplateInputException: