try catch finally Exception Handling Java

try catch finally Exception Handling Java

try catch finally – The finally Block

We know earlier, there should be a successful handler in the catch block like ArithmeticException for division by zero and ArrayIndexOutOfBoundsException for wrong array index number etc. Placing a wrong handler is equivalent to not having a try-catch block at all. There may be some statements which must be executed before the program is terminated even if the exception is not handled successfully. Such statements include the closing of streams in I/O operations, sockets in LAN programming and JDBC (Java Database Connectivity) connections etc. These statements are known as cleanup operations. Then, how to execute the cleanup statements even if the exception is not handled properly, by chance? Java's friendliness comes once more into picture, Java gives finally keyword. finally block statements are guaranteed of execution even if the programmer fails to handle the exception successfully.

Following program illustrates the usage of finally block.


The output of the code is OK 1, as you expected, as this statement exists before the problem. Now, OK 2 is also printed as it exists within the finally block. As the exception is not handled successfully (ArithmeticException exists in place of the correct one, ArrayIndexOutOfBoundsException), the program terminates without executing OK 3. This concludes you that the finally block statements are guaranteed of execution even if the exception is not handled properly with proper handler. Place all the important statements, that requires definite execution, in the finally block.

try – catch – finally

try, catch and finally are keywords of Java used with exception handling mechanism only. Programmer can use the combination of these three in exception handling.

  1. The statements of try block may or may not raise the exception.
  2. If the try throws the exception, the catch block is executed (else, not executed). The catch block includes an exception handler and some statements used to inform the user about the possible problem.
  3. The finally block statements give guarantee of execution even if the exception handler fails to handle the exception. finally block is the best place to have the cleanup code like closing of I/O stream handles etc.

Multiple Catch Blocks

One catch block can handle only one exception; it is the best practice. There may be an occasion where the try block may have multiple problems. It is possible to have multiple catch blocks for one try block and is illustrated in the following program.

In multiple catch blocks, each catch block is checked, one after another, for a suitable handler. When one catch block handles the exception, the next catch blocks are not executed. Control shifts directly after the last catch to execute the remaining part of the program.

1. Do you know that there can be try-finally without catch block.

2. Would you like to know the rules of exceptions in method overriding.

3. For clear understanding of checked and unchecked exceptions, it is required to know the hierarchy of exceptions.

One stop destination for all Java Exception world.

3 thoughts on “try catch finally Exception Handling Java

Leave a Reply

Your email address will not be published. Required fields are marked *