Java异常体系

Understanding Java Exceptions

Introduction

In Java programming language, an exception is an event that occurs during the execution of a program, which disrupts the normal flow of the program’s instructions. When an exception occurs, the normal flow of the program is terminated, and the program execution is transferred to the exception handler. In this blog post, we will discuss the different types of Java exceptions, what causes them, and how to handle them effectively.

Types of Java Exceptions

Java exceptions are classified into two categories - checked exceptions and unchecked exceptions. Checked exceptions are those that are checked by the compiler at compile-time, while unchecked exceptions are not checked by the compiler.

Checked Exceptions

Checked exceptions are those that are checked by the compiler at compile-time. These exceptions occur due to external factors that are beyond the control of the program. For example, file I/O errors, network connection errors, and database errors are all examples of checked exceptions. When a method throws a checked exception, it must be handled by either catching the exception or declaring the exception in the method signature using the throws keyword.

Unchecked Exceptions

Unchecked exceptions are not checked by the compiler at compile-time. These exceptions occur due to program errors, such as null pointer exceptions, array index out of bounds exceptions, and arithmetic exceptions. Unchecked exceptions can be handled by using the try-catch block, or by allowing the program to terminate abnormally.

Handling Exceptions in Java

Java provides a robust exception handling mechanism to handle exceptions effectively. When an exception occurs, the program execution is transferred to the exception handler. The exception handler can then take appropriate action, such as logging the error, retrying the operation, or terminating the program gracefully.

To handle exceptions in Java, you can use the try-catch block. The try block contains the code that may throw an exception, while the catch block contains the code that handles the exception. If an exception is thrown in the try block, the control is transferred to the catch block. You can also use the finally block to execute code that must be executed, regardless of whether an exception is thrown or not.

Conclusion

In conclusion, Java exceptions are an essential part of Java programming. They allow programs to handle errors and unexpected events effectively. By understanding the different types of Java exceptions and how to handle them, you can write more robust and reliable Java programs.

理解Java异常

简介

在Java编程语言中,异常是程序执行过程中出现的事件,它会打断程序正常的指令流程。当异常发生时,程序的正常流程终止,程序执行被转移到异常处理程序。在本篇博客文章中,我们将讨论Java异常的不同类型、引起它们的原因以及如何有效地处理它们。

Java异常类型

Java异常分为两类 - 已检查异常和未检查异常。已检查异常是编译器在编译时检查的异常,而未检查异常则不受编译器检查。

已检查异常

已检查异常是编译器在编译时检查的异常。这些异常是由程序无法控制的外部因素引起的。例如,文件I/O错误、网络连接错误和数据库错误都是已检查异常的例子。当一个方法抛出一个已检查异常时,它必须被处理,要么通过捕获异常,要么通过在方法签名中使用throws关键字声明异常。

未检查异常

未检查异常是编译器在编译时不检查的异常。这些异常是由程序错误引起的,比如空指针异常、数组索引超出界限异常和算术异常。未检查异常可以通过使用try-catch块处理,或者允许程序异常终止。

Java中的异常处理

Java提供了强大的异常处理机制来有效地处理异常。当异常发生时,程序执行被转移到异常处理程序。异常处理程序可以采取适当的行动,例如记录错误、重试操作或优雅地终止程序。

要在Java中处理异常,可以使用try-catch块。try块包含可能引发异常的代码,而catch块包含处理异常的代码。如果try块中抛出异常,则控制转移到catch块。您还可以使用finally块执行必须执行的代码,无论是否抛出异常。

结论

总之,Java异常是Java编程的重要组成部分。它们允许程序有效地处理错误和意外事件。通过理解不同类型的Java异常以及如何处理它们,您可以编写更健壮和可靠的Java程序。

类继承关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
                     ┌───────────┐
│ Object │
└───────────┘


┌───────────┐
│ Throwable │
└───────────┘

┌─────────┴─────────┐
│ │
┌───────────┐ ┌───────────┐
│ Error │ │ Exception │
└───────────┘ └───────────┘
▲ ▲
┌───────┘ ┌────┴──────────┐
│ │ │
┌─────────────────┐ ┌─────────────────┐┌───────────┐
│OutOfMemoryError │... │RuntimeException ││IOException│...
└─────────────────┘ └─────────────────┘└───────────┘

┌───────────┴─────────────┐
│ │
┌─────────────────────┐ ┌─────────────────────────┐
│NullPointerException │ │IllegalArgumentException │...
└─────────────────────┘ └─────────────────────────┘

Java规定:

  • 必须捕获的异常,包括Exception及其子类,但不包括RuntimeException及其子类,这种类型的异常称为Checked Exception。
  • 不需要捕获的异常,包括Error及其子类,RuntimeException及其子类。

编译器对RuntimeException及其子类不做强制捕获要求,不是指应用程序本身不应该捕获并处理RuntimeException。是否需要捕获,具体问题具体分析。

参考链接

https://www.liaoxuefeng.com/wiki/1252599548343744/1264734349295520

https://www.runoob.com/java/java-exceptions.html