17.2 Exception Handling and the Exception Hierarchy in Java

Exception handling in Java is one of the fundamental pillars for building robust and reliable applications. Exceptions are events that occur during the execution of a program that interrupt the normal flow of instructions. When an exception occurs, it is important to have mechanisms in place to catch and handle it appropriately, thus preventing the program from terminating abruptly and possibly leaving the system in an inconsistent state.

Try-Catch-Finally Blocks

The primary mechanism for handling exceptions in Java is composed of the try, catch and finally blocks. Let's understand how each of them works:

  • try: A try block is used to wrap code that can potentially throw an exception. If an exception is thrown within the try block, execution of the block is stopped and control is passed to the corresponding catch block.

  • catch: The catch block is used to catch and handle exceptions. It is possible to have multiple catch blocks after a single try block, each catching a specific type of exception. Program execution continues normally after the catch block is executed.

  • finally: The finally block is optional and is executed after the execution of the try and catch blocks, regardless of whether an exception was thrown or not. This block is generally used to close resources or free memory, ensuring that certain operations are performed even if an exception occurs.

A classic example of using these blocks is reading a file:


try {
    // Code that can throw an exception, such as opening a file
} catch (FileNotFoundException e) {
    // Handling the case where the file is not found
} catch (IOException e) {
    // Handling for other I/O exceptions
} finally {
    // Close resources, such as a file or network connection
}

Exception Hierarchy

Exceptions in Java are objects and follow a class hierarchy. The base class for all exceptions is Throwable, which has two direct subclasses: Error and Exception.

  • Error: Represents serious problems that an application should not normally attempt to handle. Errors are used by the Java Virtual Machine (JVM) to indicate serious problems that are not directly related to the program logic, such as OutOfMemoryError, indicating that the JVM has no more memory available.

  • Exception: Represents conditions that an application can reasonably attempt to recover from. Within Exception, there are two main categories: checked exceptions and unchecked exceptions.

Checked exceptions are those that must be caught or declared in the method that throws them, while unchecked exceptions, which inherit from RuntimeException, do not need to be explicitly declared or caught. Unchecked exceptions often indicate programming errors, such as trying to access an index outside the bounds of an array (ArrayIndexOutOfBoundsException).

It is important to carefully design the exception handling strategy in a Java application. Here are some best practices:

  • Use checked exceptions for recoverable problems and unchecked exceptions for programming errors.
  • Avoid catching generic exceptions like Exception or Throwable, as this can catch unexpected exceptions, making debugging difficult.
  • Use finally blocks to free up resources such as input/output streams and database connections.
  • When you catch an exception, do something useful with it, such as log the error or take corrective action.
  • Avoid excessive use of try-catch blocks as this can make the code difficult to read and maintain.

To conclude, exception handling is essential for creating stable and reliable Java programs. Understanding the exception hierarchy and knowing how to use try-catch-finally blocks effectively is critical for any Java developer. By following best practices, you can ensure that your code is prepared to handle unexpected situations without compromising the integrity of the program.

Now answer the exercise about the content:

Which of the following statements about exception handling in Java is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Exception handling and the exception hierarchy in Java: Throwing exceptions

Next page of the Free Ebook:

105Exception handling and the exception hierarchy in Java: Throwing exceptions

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text