Unchecked Exceptions in Java

12 Apr 2025 | 8 分钟阅读

在 Java 中,**运行时异常**也被称为**运行时异常**。运行时异常是一类不需要使用 throws 关键字在方法签名中声明的异常。

它们继承自 RuntimeException 类,而 RuntimeException 类本身又是 Exception 类的子类。运行时异常在编译时不会被检查,因为它们是在运行时由于逻辑错误而产生的。

编程错误,如错误的用​​户输入、API 使用不当、空指针引用和算术错误,通常是运行时异常的原因。它们指出了软件中的逻辑错误,最好通过使用适当的编码和验证过程来避免。

Unchecked Exceptions in Java

运行时异常的特点

  1. 无编译时检查:与已检查异常不同,运行时异常在编译时不会被检查。
  2. RuntimeException 的后代:所有运行时异常都继承自 RuntimeException 类。
  3. 运行时发生:这些异常仅在程序运行时发生。
  4. 可选处理:不强制使用 try-catch 块处理运行时异常,也不需要在 throws 子句中声明它们。
  5. 通常表示编程错误:它们通常由逻辑错误引起,例如除以零、访问空引用或数组越界索引。

运行时异常的类型

运行时异常涵盖了 Java 中广泛的运行时错误。以下是一些最常见的:

  1. NullPointerException
  2. ArrayIndexOutOfBoundsException
  3. ArithmeticException
  4. ClassCastException
  5. IllegalArgumentException
  6. NumberFormatException
  7. InputMismatchException
  8. IllegalStateException

1. NullPointerException

NullPointerException 是 Java 应用程序执行时最常见的运行时错误之一。当代码尝试使用一个 null 对象的引用时,就会发生这种情况。理解此异常的工作原理对于调试和提高代码的可靠性至关重要,因为不正确的处理可能导致意外行为。

NullPointerException 示例

当我们执行上述程序时,会得到一个 NullPointerException。

2. ArrayIndexOutOfBoundsException

当尝试访问小于 0 或大于数组最大长度的数组索引时,会发生 ArrayIndexOutOfBoundsException 运行时异常。当尝试访问数组元素时会出现此问题。

在 Java 中,数组的长度是固定的(从 0 到 length – 1);因此,访问超出分配空间的索引会导致异常。

ArrayIndexOutOfBoundsException 示例

当我们执行上述程序时,会得到一个 ArrayIndexOutOfBoundsException。

3. ArithmeticException

ArithmeticException 是一种运行时异常,在执行非法算术运算时发生。最常见的原因是整数运算中的除以零。由于它是 RuntimeException 的子类,因此不需要显式声明或处理,但如果放任不管,可能会破坏程序执行。

要阅读更多 Java 中的 ArithmeticException

ArithmeticException 示例

当我们执行上述程序时,会得到一个 ArithmeticException。

4. ClassCastException

ClassCastException 是一种运行时异常,当尝试将对象强制转换为不兼容的类或类型时发生。当 JVM 检测到强制转换无效且无法安全执行时,会抛出此异常。

要阅读更多 Java 中的 ClassCastException

ClassCastException 示例

当我们执行上述程序时,会得到一个 ClassCastException。

5. IllegalArgumentException

当方法接收到的参数逻辑上不合理时,会抛出 IllegalArgumentException。它派生自 RuntimeException,因此在编译时不会被编译器检查;只会在运行时检查。该异常用于指示方法收到了非法或不合适的参数。

要阅读更多 Java 中的 IllegalArgumentException

IllegalArgumentException 示例

当我们执行上述程序时,会得到一个 IllegalArgumentException。

6. NumberFormatException

当我们尝试将字符串转换为数字(如 int、double 或 float)但字符串不是有效的数字值时,会发生 NumberFormatException。例如,将字符串“xyz”转换为整数会抛出 NumberFormatException。

要阅读更多 Java 中的 NumberFormatException

NumberFormatException 示例

输出

 
Exception in thread "main" java.lang.NumberFormatException: Cannot parse null string
	at java.base/java.lang.Integer.parseInt(Integer.java:623)
	at java.base/java.lang.Integer.parseInt(Integer.java:777)
	at Main.main(Main.java:3)   

7. InputMismatchException

当用户输入的输入与预期类型不匹配时,会发生 InputMismatchException。例如,如果我们期望一个 int 但用户输入了一个 String 或一个超出范围的输入。

InputMismatchException 示例

当我们执行上述程序时,会得到一个 InputMismatchException。

8. IllegalStateException

当代码的方法在错误的时间被触发或调用时,会发生 IllegalStateException。此异常用于发出方法调用时间不正确的信号。

要阅读更多 Java 中的 IllegalStateException

IllegalStateException 示例

当我们执行上述程序时,会得到一个 IllegalStateException。

处理运行时异常

我们可以使用 try-catch 块来处理运行时异常。
要阅读更多 try-catch 块

处理 ArrayIndexOutOfBoundsException

示例

编译并运行

输出

 
Out of index. Check the code.   

处理 NullPointerException

示例

编译并运行

输出

 
The string object should not be null.   

处理 InputMismatchException

示例

编译并运行

输出

 
Enter name: Andrew
Enter age: 28
Andrew 28   

处理 ArithmeticException

示例

编译并运行

输出

 
Should avoid dividing by 0 java.lang.ArithmeticException: / by zero  

处理 NumberFormatException

示例

编译并运行

输出

 
Invalid string in argument

处理 IllegalStateException

示例

编译并运行

输出

 
Main thread is going to sleep
This is an example of an IllegalStateException
This is an example of an IllegalStateException
This is an example of an IllegalStateException
Main thread is awakened
This is the main thread

何时使用运行时异常?

当开发人员不想解决错误时,应使用运行时异常。开发人员认为,如果发生运行时异常,系统不会崩溃。它用于在无法处理错误时捕获已检查异常。