Free Ebook cover Learn to program in complete Java, from programming logic to advanced

Learn to program in complete Java, from programming logic to advanced

3.5

(4)

238 pages

Exception handling and the exception hierarchy in Java: Types of exceptions (checked and unchecked)

Capítulo 106

Estimated reading time: 5 minutes

Audio Icon

Listen in audio

0:00 / 0:00

17.4. Exception Handling and the Exception Hierarchy in Java

Exception handling is a fundamental aspect of Java programming. An exception is an event that occurs during the execution of a program and that interrupts the normal flow of instructions. The exception mechanism in Java provides a way to separate error-handling code from regular code and allows a method to return information about problems occurring during execution.

Types of Exceptions in Java

In Java, exceptions are divided into two main categories: checked exceptions and unchecked exceptions.

Checked Exceptions

checked exceptions are those that the compiler requires to be handled or declared in the method that generates them. They are used for problems that the programmer must anticipate and handle appropriately, these are generally problems outside the control of the program, such as I/O (Input/Output) problems, network failures, etc. Examples of checked exceptions include IOException, SQLException and ClassNotFoundException.

Unchecked Exceptions

unchecked exceptions include the subclasses of Error and RuntimeException. These are exceptions that the compiler does not require to be handled or declared, and are generally the result of errors in the code itself, such as accessing an index outside the bounds of an array (ArrayIndexOutOfBoundsException) or trying to access an object through of a null reference (NullPointerException).

Exception Hierarchy in Java

The base class for all exceptions in Java is Throwable. From it, two important subclasses derive: Error and Exception. The Error class is designed for situations that are not normally expected to be caught or handled by your program, such as OutOfMemoryError or StackOverflowError. The Exception class is the basis for all exceptions that can be handled and has two main categories: checked exceptions and unchecked exceptions, as mentioned previously.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Exception Handling

Exception handling in Java is done through try, catch and finally blocks. The try block contains the code that can throw an exception, while the catch block is used to catch and handle the exception. The finally block, which is optional, contains code that is always executed after the try block, regardless of whether an exception was thrown or not.

try {
    // code that can throw an exception
} catch (ExcecaoType1 e) {
    // treatment for TypeExcecao1
} catch (ExceptionType2 e) {
    // treatment for TypeExcecao2
} finally {
    // code that is always executed after the try block, with or without exceptions
}

It is possible to catch multiple exceptions in a single catch block using the pipe operator (|), this is useful when the same block of code can generate different exceptions, but the handling for all of them is the same.

try {
    // code that can throw different exceptions
} catch (TypeExcecao1 | TypeExcecao2 e) {
    // common treatment for TypeExcecao1 and TypeExcecao2
}

Exception Propagation

When an exception is thrown and is not caught in the same method, it propagates up the method call stack until it finds a catch block that can handle it. If no suitable catch block is found, the program is terminated and the call stack is printed, which helps in diagnosing the problem.

Good Practices in Exception Handling

When it comes to exceptions, it is important to follow some good practices:

  • Only handle exceptions that you can actually handle.
  • Avoid catching Throwable, Exception or RuntimeException unless absolutely necessary, as this can catch unforeseen exceptions and hide bugs.< /li>
  • Do not use exceptions for flow control in your program.
  • Provide helpful, detailed error messages.
  • Clean up resources in a finally block, or use the try-with-resources feature from Java 7 onwards to ensure that resources are automatically closed after use.

Effective exception handling is essential for building robust and reliable programs. By understanding exception types, the exception hierarchy, and best practices, you will be well-equipped to deal with challenges that arise when programming in Java.

Now answer the exercise about the content:

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

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

You missed! Try again.

Option 2 is correct. Java allows catching multiple exceptions in a single catch block using the pipe (|) operator. This is beneficial when multiple exceptions lead to similar handling code, allowing cleaner and more efficient error management.

Next chapter

Exception Handling and the Exception Hierarchy in Java: Exception Hierarchy in Java

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.