ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java异常体系

2021-11-19 22:02:47  阅读:213  来源: 互联网

标签:体系 java RuntimeException 子类 Error exceptions Throwable 异常


在这里插入图片描述
​ 上述是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捕获)

标签:体系,java,RuntimeException,子类,Error,exceptions,Throwable,异常
来源: https://blog.csdn.net/SprintMan/article/details/121428061

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有