Exceptions in Java iii: Control Flow and Multiple try, catch, finally

Java supports multiple non-mandatory catch blocks and a finally block. There are strict rules to be followed to decide the order and presence of these blocks. We have already looked at introduction to exceptions and basic try-catch-finally-mechanism. We now look at the control flow of these three blocks.

Control Flow of try-catch-finally Blocks

There are multiple usage patterns of try-catch-finally block. We now analyze them one by one starting with the simplest.

One try and One catch

This is is the simplest usage pattern.Java

The above code block is telling Java to execute code in the try block and if it encounters an Exception to execute the code in catch block. The control flow moves from the try block to catch block as soon as exception is encountered. In the above example, punish(aCitizen) executes fine, when punish(aPolitician) throws an exception the control flow moves from the try block to catch block. The System.out.println() in the try block is never executed.

Remember that catch block is executed only when exception occurs. Otherwise, the control flow proceeds from the try block to the statement immediately after catch block.

One try and Multiple catch

Java allows you to specify multiple catch blocks for a try block. There is one golden rule that compiler forces you to adhere to in this regard. The catch blocks must be ordered based on the hierarchy. catch block for a specific exception must be placed before catch block for a more general exception. We will be covering exception hierarchies in great details in the next lesson.Java

The above code prints

Note that it is compile time error to try to reverse the order of catch blocks. Since Exception is a sub-class of Throwable, the catch block of Exception must be placed before that of Throwable. The reason for this rule has to do with is-a relationship between Throwable and Exception. Exception is-a Throwable. If you place Throwable‘s catch block before Exception‘s Java has no way of deciding which block it must execute. We will cover the is-a relationship further in our lessons on object orientd programming.

A try and a finally

Depending on how bleeding-edge your coding practices are, this may either be the way to write code or a big no-no.

Traditionally, Java programmers never write code that has just a try and finally. The conventional wisdom states that “catch the exception closest to its point of origin.” Therefore, in the old-school Java, you would always write a catch block for try.

Not writing a catch block lets the exception propagate. This propagation can be handled elsewhere. This is the latest wave in exception handling. You catch only what can be meaningfully handled.

Here is sample code for this usage pattern.Java

The output of the above code is

One try, multiple catch and a finally

This is the most complete variant that specifies all three – try, catch and finally. There are no additional rules apart from the ones that we have covered. The following code demonstrates this usage pattern.

The output of the above code is

Nested try-catch-finally

Java allows you to nest a try-catch-block within other try, catch and finally blocks. For example you can have a try-catch-finally block inside a catch or a finally or within other try block

The output of this code is

Having seen the rules for usage of try, catch and finally we will summarize all the rules in the next article.

Leave a comment

Your email address will not be published.