What is the use of finally block in exception handling in Java?

Java's finally block is useful in exception handling and always used in conjunction with try block. There should at least be one try block that finally block can be associated to. The try encloses a block of code in which exception may occur. When a piece of code throws an exception, it stops processing the remaining code in try block and transfers the control to an appropriate catch block if a catch block exists (catch block is optional), else control gets transferred to finally block. Both catch and finally are optional, you can omit one of them at a time but not both. Also, there can be more than one catch block associated to a single try block but only one catch block is processed at a time. Also note that the catch blocks immediately follow the try block.

So, exceptions are thrown from try block and caught by catch block then why finally needed and what is its purpose? Let us see that. Upon occurrence of an exception a method may decide to exit, which could be problematic, in case, the method has acquired some local resource that only it knows about and if that resource must be cleaned up before exiting the method. To tackle this problem Java provides finally clause. However, you can place the clean up code in catch blocks, but exception handlers (catch blocks) are a poor place to clean up after the code in the try block because each handler then requires its own copy of the clean up code. If, for example, you allocated a network resource or opened a file somewhere in the try block, each exception handler would have to close the file or release the resource. That would definitely lead to a lot of redundant code. To get rid of this problem, Java offers the finally block.

Piece of code contained by finally block is executed at some point after the try block, whether an exception is thrown or not. Even if there is a return statement in the try block, the finally block gets executed right after the return statement is encountered, and before the return gets executed.

If the try block gets executed with no exceptions, the finally block is executed immediately after the try block completes. If there was an exception thrown, the finally block gets executed immediately after the proper catch block completes. The finally block will not be executed if exit() is called before finally block is reached. The exit() call will shutdown the JVM, so no subsequent line of code will be run.

Remember, finally clauses are optional. If you don't code one, your program will compile and run just fine. You need not to code finally block if you have no resources to clean up. There will always be only one finally clause associated to a try block. In case of multiple try blocks you can have multiple finally clauses ensuring their proper association to try statements.

In Java 7, the try-with-resources statement is introduced. This is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Hope you have enjoyed reading the use of finally block in exception handling in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.