Yige

Yige

Build

Exceptions in Java

Exception Handling in Java#

Classification of Exceptions#

Java handles exceptions in an object-oriented manner, categorizing them by different types. The base class for all exception classes is the Throwable class, with two main subclasses: Error and Exception:

  • Error refers to errors that the program cannot handle, such as OOM, etc.
  • Exception refers to exceptions that the program can handle, divided into two major categories: RuntimeException and Checked Exception. Checked exceptions need to be handled using the try, catch, and finally keywords at compile time; otherwise, the compiler will throw an error.
    image.png

Execution of the finally Block#

The code block in finally will be executed regardless of whether an exception is caught. When involving break and continue statements, the finally clause will also be executed. In the following special cases, the finally block will not be executed:

  • An exception occurs in the finally block.
  • System.exit() is used in the preceding code or the program has already returned and exited.
  • The thread in which the program is running dies.
  • The CPU is shut down.

Execution of return and finally#

Reference: Java Basics - The Order of Execution of finally and return

  • The return statement is executed first, but it does not return immediately; instead, it executes the finally statement, and after the finally statement has finished executing, the return statement returns the result!
  • The return statement in the finally block will override the return in the try block.

What is the difference between throw and throws?#

  • throw is used within a method, while throws is used in the method declaration.
  • throw is followed by an exception object, while throws is followed by an exception type.
  • throw can only follow one exception object, while throws can declare multiple exception types at once.

What is the difference between final, finally, and finalize?#

  • final: It is a modifier. If it modifies a class, that class cannot be inherited; if it modifies a method or variable, it indicates that this method and variable cannot be changed and can only be used.
  • finally: It is the last part of try{} catch{} finally{}, indicating that it will be executed regardless of any situation. The finally part can be omitted, but if it exists, the code inside finally will definitely be executed.
  • finalize: It is a method of the Object class, called when the garbage collector executes on the object being collected.

Exception Handling#

Reference from Alibaba's "Java Specification Manual"

  • Do not catch runtime exception classes defined in the Java library that inherit from RuntimeException, such as IndexOutOfBoundsException / NullPointerException. These exceptions should be pre-checked by the programmer to ensure the robustness of the program.
  • Catching exceptions is for handling them; do not catch them and do nothing, discarding them.
  • If a try block is placed in transactional code, after catching an exception, if a rollback is needed, be sure to pay attention to manually rolling back the transaction.
  • Do not use return in the finally block; returning from the finally block ends the execution of the method, and the return statement in the try block will not be executed.
  • Accurately distinguish exception types to avoid using try-catch on large blocks of code and avoid throwing Exception.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.