Exceptions in Java II: try, catch, finally

Java’s exception handling constructs comprise of try, catch, throw, throws and finally. These statements together constitute  the main pillars of Java’ exception handling mechanism. In the last lesson we have already seen what is a Java exception. We now turn to mechanisms for handling exceptions.

A try statement encapsulates code that is likely to throw an exception. A catch statement specifies the code to be executed when an exception occurs. A finally statement specifies code to be executed immaterial of whether an exception occurs or not.

Generally, these constructs are used in conjunction as shown below. But only the try part is mandatory. Between catch and finally statements you need to have minimum one statement.Java

The try Block

The try block is a code block containing code that may follow multiple execution flow. This code block like all code blocks starts with a ‘{‘ .

There is a one happy path – the main control path of the program and multiple alternate ( and usually sadder) paths. The try block contains the code for the main, happy path. The alternate paths are put in catch blocks.

The catch Block

The catch block specifies code to execute when your main code in the try block cannot be executed. Since there can be multiple scenarios where your main code is unable to execute further, Java allows you to associate multile catch blocks with a try block. Further, there may be a scenario where there is nothing you can do in response to special situations. Therefore, Java allows you to attach zero or more catch blocks to a try block. A catch block must always be associated with a try block, but a try block need not always be associated with a catch block.

The syntax of the multiple catch blocks is as follows

We will discuss the order of multiple catch blocks in the next lesson.

The finally block

The finally block specifies code that must be executed immaterial whether exception is raised or not. You can think of it as code statements that are common to the main code path and alternate paths specified by the catch blocks. The finally block pretty much like catch block is always attached to a try block. There can be either zero or one finally blocks associated to a try block.

A finally block is many a times used for cleanup and freeing resources. If you allocated some resources (such as you opened a network socket or database connection or a file) at the beginning of try block you need to de-allocate them immaterial of whether exception occurs or not. The finally block is the place to such mandatory code.

Does a finally Block Always Run?

No.

A finally block is guaranteed to run only in ‘ordinary’ situations. Extra-ordinary situations such as the ones listed below can cause finally block to not execute

  1. You call System.exit() in the code before finally block. Note that a return statement prior to finally block does not prevent the execution of the block.
  2. Your thread executing the code gets a stop() [now deprecated] method invoked on it
  3. Your thread gets a suspend() call and the corresponding resume() never happens
  4. JVM comes crashing down. JVM may crash because of unhandled exceptions, running out of memory and a myriad of other reasons including volcanoes erupting in your neighborhood.
    When JVM crashes, your OS must do the cleanup for the Java process that was running the JVM. You should not be bothered about this scenario. Because finally block is mainly for cleanup and  you don’t put your core business logic in the finally block, there is nothing more to do. Cleanup of resources will now be performed by your underlying operating systems.
    Remember that if an exception is thrown in finally block and is unhandled, the rest of the JVM crashes and the rest of the finally block does not execute.

An Example of try, catch and finally

We turn to our favorite example of a Politician to demonstrate the usage of try-catch-finally.Java

In the above code we have exactly 1 catch and 1 finally block. The finally block has code that executes in all circumstances. In the next article we discuss multiple catch statements and flow control in greater detail.

Leave a comment

Your email address will not be published.