RuntimeException 子类

简介: 本文简要介绍RuntimeException及其子类

RuntimeException子类

有时候总是会区分不清哪些异常类是RuntimeException的子类,这里特意去源码里面找来整理一下,方便后续查阅,由于RuntimeException在java.lang包下,所以这里也只介绍java.lang包下的RuntimeException子类,通常java.lang包下的异常子类也是遇到比较多的

image.png下面逐一介绍每个子类发生的场景

ArithmeticException

当出现异常的运算条件时,抛出此异常。例如,一个整数"除以零"时,抛出此类的一个实例。

Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.

ArrayIndexOutOfBoundsException

用非法索引访问数组时抛出的异常。如果索引为负或大于等于数组大小,则该索引为非法索引。

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

ArrayStoreException

试图将错误类型的对象存储到一个对象数组时抛出的异常。

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an  

ArrayStoreException:

 
    

   Object x[] = new String[3];

   x[0] = new Integer(0);

ClassCastException

当试图将对象强制转换为不是实例的子类时,抛出该异常。

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a  

ClassCastException:

 
    

   Object x = new Integer(0);

   System.out.println((String)x);

EnumConstantNotPresentException

Thrown when an application tries to access an enum constant by name and the enum type contains no constant with the specified name.

This exception can be thrown by the {@linkplain java.lang.reflect.AnnotatedElement API used to read annotations reflectively}.

IllegalArgumentException

抛出的异常表明向方法传递了一个不合法或不正确的参数。

Thrown to indicate that a method has been passed an illegal or inappropriate argument.

IllegalMonitorStateException

抛出的异常表明某一线程已经试图等待对象的监视器,或者试图通知其他正在等待对象的监视器而本身没有指定监视器的线程。

Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.

IllegalStateException

在非法或不适当的时间调用方法时产生的信号。换句话说,即 Java 环境或 Java 应用程序没有处于请求操作所要求的适当状态下。

Signals that a method has been invoked at an illegal or inappropriate time.  In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

IllegalThreadStateException

线程没有处于请求操作所要求的适当状态时抛出的异常。

Thrown to indicate that a thread is not in an appropriate state for the requested operation. See, for example, the

suspend and resume methods in class Thread.

IndexOutOfBoundsException

指示某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出。

Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

NegativeArraySizeException

如果应用程序试图创建大小为负的数组,则抛出该异常。

Thrown if an application tries to create an array with negative size.

NullPointerException

当应用程序试图在需要对象的地方使用 null 时,抛出该异常

Thrown when an application attempts to use {@code null} in a case where an object is required. These include:

  • Calling the instance method of a {@code null} object.
  • Accessing or modifying the field of a {@code null} object.
  • Taking the length of {@code null} as if it were an array.
  • Accessing or modifying the slots of {@code null} as if it
  •    were an array.

  • Throwing {@code null} as if it were a {@code Throwable}
  •    value.

    Applications should throw instances of this class to indicate other illegal uses of the {@code null} object.

    {@code NullPointerException} objects may be constructed by the virtual machine as if {@linkplain Throwable#Throwable(String, Throwable, boolean, boolean) suppression were disabled and/or the stack trace was not writable}.

    NumberFormatException

    当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常。

    Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

    SecurityException

    由安全管理器抛出的异常,指示存在安全侵犯。

    Thrown by the security manager to indicate a security violation.

    StringIndexOutOfBoundsException

    此异常由 String 方法抛出,指示索引或者为负,或者超出字符串的大小。

    Thrown by {@code String} methods to indicate that an index is either negative or greater than the size of the string.  For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.

    TypeNotPresentException

    Thrown when an application tries to access a type using a string representing the type's name, but no definition for the type with the specified name can be found.   This exception differs from {@link ClassNotFoundException} in that ClassNotFoundException is a checked exception, whereas this exception is unchecked.

    Note that this exception may be used when undefined type variables are accessed as well as when types (e.g., classes, interfaces or annotation types) are loaded.

    In particular, this exception can be thrown by the {@linkplain java.lang.reflect.AnnotatedElement API used to read annotations reflectively}.

    UnsupportedOperationException

    当不支持请求的操作时,抛出该异常。

    Thrown to indicate that the requested operation is not supported.

    如何通过IDEA查找类及子类源码

    我用的是IDEA 2020版本,首先全局搜索你想要查找的父类,比如 RuntimeException

    image.png

    Ctrl+鼠标左键点击1处的RuntimeException,可以直接RuntimeException类

    image.png

    点击红框可以看到所有继承RuntimeException的子类,包括我们想要看的java.lang包下的子类

    image.png

    这个时候点击1处可以看到,

    image.png

    下滑即可找到我们想要看的java.lang下的RuntimeException的子类,同样的方法也可以去查看Exception的子类,如图

    image.png

    通过阅读源码可以让我们更深入的理解父类子类关系,在工作中多看多用,知识自然就记忆不忘了。






    相关文章
    |
    前端开发 JavaScript 数据安全/隐私保护
    详解React antd中setFieldsValu的简便使用
    详解React antd中setFieldsValu的简便使用
    772 0
    |
    10月前
    |
    数据采集 Java API
    深度解析:爬虫技术获取淘宝商品详情并封装为API的全流程应用
    本文探讨了如何利用爬虫技术获取淘宝商品详情并封装为API。首先介绍了爬虫的核心原理与工具,包括Python的Requests、BeautifulSoup和Scrapy等库。接着通过实战案例展示了如何分析淘宝商品页面结构、编写爬虫代码以及突破反爬虫策略。随后讲解了如何使用Flask框架将数据封装为API,并部署到服务器供外部访问。最后强调了在开发过程中需遵守法律与道德规范,确保数据使用的合法性和正当性。
    |
    机器学习/深度学习 人工智能 测试技术
    昆仑万维开源 Skywork R1V:开源多模态推理核弹!视觉链式分析超越人类专家
    Skywork R1V 是昆仑万维开源的多模态思维链推理模型,具备强大的视觉链式推理能力,能够在多个权威基准测试中取得领先成绩,推动多模态推理模型的发展。
    361 4
    昆仑万维开源 Skywork R1V:开源多模态推理核弹!视觉链式分析超越人类专家
    |
    SQL 关系型数据库 MySQL
    mysql下出现Unknown column ‘xx‘ in ‘on clause‘的完全解决方法
    mysql下出现Unknown column ‘xx‘ in ‘on clause‘的完全解决方法
    1037 0
    |
    SQL XML Java
    八、(了解即可)MyBatis懒加载(或者叫延迟加载)
    八、(了解即可)MyBatis懒加载(或者叫延迟加载)
    408 1
    |
    存储 C语言
    C语言:普通局部变量、普通全局变量、静态局部变量、静态全局变量的区别
    C语言中,普通局部变量在函数内部定义,作用域仅限于该函数;普通全局变量在所有函数外部定义,作用域为整个文件;静态局部变量在函数内部定义但生命周期为整个程序运行期;静态全局变量在所有函数外部定义,但仅在定义它的文件内可见。
    1368 10
    |
    存储 SQL 大数据
    从数据存储到分析:构建高效开源数据湖仓解决方案
    今年开源大数据迈向湖仓一体(Lake House)时代,重点介绍Open Lake解决方案。该方案基于云原生架构,兼容开源生态,提供开箱即用的数据湖仓产品。其核心优势在于统一数据管理和存储,支持实时与批处理分析,打破多计算产品的数据壁垒。通过阿里云的Data Lake Formation和Apache Paimon等技术,用户可高效搭建、管理并分析大规模数据,实现BI和AI融合,满足多样化数据分析需求。
    |
    算法 关系型数据库 MySQL
    mysql忘记密码怎么办(附免密登录和修改密码)
    mysql忘记密码怎么办(附免密登录和修改密码)
    7320 0
    mysql忘记密码怎么办(附免密登录和修改密码)
    |
    Java Docker 容器
    Docker SpringBoot Skywalking 8.1 agent
    Docker SpringBoot Skywalking 8.1 agent
    410 0
    |
    JSON 前端开发 fastjson
    成功解决:java.util.LinkedHashMap cannot be cast to com.zyz.bookshopmanage.pojo.GoodsInfo
    这篇文章讲述了在Java后端开发中遇到的类型转换错误,即无法将`java.util.LinkedHashMap`转换为`com.zyz.bookshopmanage.pojo.GoodsInfo`对象的问题。文章提供了解决这个问题的两种方法:一是将对象转换为JSON字符串再反序列化为对象,二是通过在项目的pom文件中引入fastjson库来简化转换过程。最后,文章展示了成功转换对象的代码示例。
    成功解决:java.util.LinkedHashMap cannot be cast to com.zyz.bookshopmanage.pojo.GoodsInfo

    热门文章

    最新文章

    下一篇
    开通oss服务