Chapter 11 - Java for Beginners Course

Try/Catch/Finally Syntax

By: Andres Perez

As mentioned in Chapter 3, exceptions are events triggered when an error occurs inside a method. The try block allows us to manage these exceptions and decide what the program should do in the event that an error occurs and an exception is thrown.

The try/catch block

In Java, exceptions are handled using the try block in its different variations. Inside the try block, we write the code that might generate an exception. In this way, we can define what should happen when an exception occurs and handle it accordingly.

As shown in Chapter 3, one of the variations of the try block is the try/catch block, which has the following structure:

try {
    // block of code we want to execute with exception handling
    // a.k.a the `try block`
} catch(ExceptionType ex) {
    // block of code that will be executed if an exception of
    // type `ExceptionType` is thrown from inside the try block
    // a.k.a the `catch block`
}

In this case, the try block contains the part of the code in which we want to handle the exceptions. If an exception is caught, the execution of the code inside the try block is stopped and instead the code inside the catch block starts executing.

The catch block receives as an argument the exception object that is thrown inside the try block. You have to specify the type of exception that the catch block will be catching. For example, if you are doing integer division inside the try block, you might want to catch an exception of type ArithmeticException in case a division by 0 is done.

The catch block will handle all exceptions of the specified type, and also of all types derived from it. So if you specify the exception type to be RuntimeException, it will also catch all types of exceptions that are derived from it, like ArithmeticException.

You can create your own types of exceptions as classes that extend the Exception class or that extend any subtype of the Exception class.

The try/finally block

Another variation of the try block is the try/finally block, in which we put code that must be executed whether an exception is thrown inside the try block or not.

It has the following structure:

try {
    // block of code we want to execute with exception handling
    // a.k.a the `try block`
} finally {
    // block of code that will always be executed,
    // regardless if an exception occurs or not inside the try block
    // a.k.a the `finally block`
}

In this way, the code we write inside the finally block will always run. If no exception occurs inside the try block, the code in the finally block is executed right after the try block.

On the other hand, if an exception occurs, execution of the code inside the try block stops, then the code inside the finally block is executed. After the code inside the finally block finishes executing, the general execution of the method stops because of the unhandled exception that was thrown.

The try/catch/finally block

It is possible to use both a catch and a finally block with the same try block. In this case, the catch statement will work just like before when used as a try/catch block. The finally block will always be executed, whether an exception occurs or not inside the try block, and even if an exception is caught.

It has the following structure:

try {
    // block of code we want to execute with exception handling
    // a.k.a the `try block`
} catch (ExceptionType ex) {
    // block of code that will be executed if an exception of
    // type `ExceptionType` is thrown from inside the try block
    // a.k.a the `catch block`
} finally {
    // block of code that will always be executed, regardless if an exception occurs or not inside the try block
    // a.k.a the `finally block`
}

The code inside the blocks will be executed differently if an exception occurs inside the try and depending on how it is handled:

  • If no exception occurs, the code inside the finally block is executed right after the code in the try block. Then the code after the try/catch/finally block is executed normally.

  • If an exception occurs and it is caught, execution of the code inside the try block stops and the code inside the catch block is executed to handle the exception. Then the finally block is executed and afterwards the code after the try/catch/finally block is executed normally.

  • If an exception occurs and it is not caught, execution of the code inside the try block stops. Then the code in the finally block is executed and after the execution is halted because of the exception.

For example, in the following code, no exception occurs, so the catch block is never executed:

// No exception occurs
try {
    System.out.println("Try block executed");
    int x = 12/4;
} catch (ArithmeticException e) {
    System.out.println("Catch block executed");
    System.out.println("An ArithmeticException occurred: " + e.getMessage());
} finally {
    System.out.println("Finally block executed");
}
System.out.println("After try/catch/finally block");

Output:

Try block executed
Finally block executed
After try/catch/finally block

On the other hand, if an exception occurs, but the catch block is able to handle it, all of the blocks are executed:

// An exception occurs and is handled
try {
    System.out.println("Try block executed");
    int x = 12/0;
} catch (ArithmeticException e) {
    System.out.println("Catch block executed");
    System.out.println("An ArithmeticException occurred: " + e.getMessage());
} finally {
    System.out.println("Finally block executed");
}
System.out.println("After try/catch/finally block. \n");

Output:

Try block executed
Catch block executed
An ArithmeticException occurred: / by zero
Finally block executed
After try/catch/finally block.

However, if no catch block is able to handle an exception that occurs inside the try block, the execution of the current method will stop, and the exception will be reported. But, since the finally block will always be executed, it runs before the execution is halted:

// An exception occurs and is handled
try {
    System.out.println("Try block executed");
    int x = 12/0;
} catch (NullPointerException e) {
    System.out.println("Catch block executed");
    System.out.println("An NullPointerException occurred: " + e.getMessage());
} finally {
    System.out.println("Finally block executed");
}
System.out.println("After try/catch/finally block. \n");

Output:

Try block executed
Finally block executed
Exception in thread "main" java.lang.ArithmeticException: / by zero
There is another form of the try block called try-with-resources. It will be covered in later chapters.