Core JAVA

finally block in Java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">The <i><b>finally <&sol;b><&sol;i>Keyword&colon;<br &sol;>&NewLine;The <b><i>finally <&sol;i><&sol;b>keyword is used to create a block of code that follows a try block&period; A finally block of code always executes&comma; whether or not an exception has occurred&period;<&sol;p>&NewLine;<p> Using a finally block allows you to run any cleanup-type statements that you want to execute&comma; no matter what happens in the protected code&period;<&sol;p>&NewLine;<p> A finally block appears at the end of the catch blocks and has the following syntax&colon;<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">try &NewLine;&lbrace; &NewLine; &sol;&sol;Protected code &NewLine;&rcub;catch&lpar;ExceptionType1 e1&rpar; &NewLine;&lbrace; &NewLine; &sol;&sol;Catch block &NewLine;&rcub;catch&lpar;ExceptionType2 e2&rpar; &NewLine;&lbrace; &NewLine; &sol;&sol;Catch block &NewLine;&rcub;catch&lpar;ExceptionType3 e3&rpar; &NewLine;&lbrace; &NewLine; &sol;&sol;Catch block &NewLine;&rcub;finally &NewLine;&lbrace; &NewLine; &sol;&sol;The finally block always executes&period; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;finally&period;jpg"&sol;><&sol;div>&NewLine;<p><b><br &sol;>&NewLine;<i>Note&colon;<&sol;i> Before terminating the program&comma; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2013&sol;02&sol;java-virtual-machine&period;html">JVM<&sol;a> executes <i>finally <&sol;i>block&lpar;if any&rpar;&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b><i>Note&colon; finally <&sol;i>must be followed by <i>try <&sol;i>or <i>catch <&sol;i>block&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>Why use finally block&quest;<&sol;b><br &sol;>&NewLine;Sometimes there may be cleanup codes&comma; which are required to be executed&period; However&comma; when an execution occurs&comma; the program halts abruptly and the clean up code never gets executed&period; To close a file before terminating a program&comma; that has encountered an exception during execution&comma; Java provides the&nbsp&semi;<i>finally<&sol;i>&nbsp&semi;block&period; The&nbsp&semi;<i>finally<&sol;i>&nbsp&semi;gets executed at runtime regardless of what happens in the&nbsp&semi;<i>try&sol;catch<&sol;i>&nbsp&semi;block&period; A&nbsp&semi;<i>finally<&sol;i>&nbsp&semi;block ensures that all cleanup work is taken care of when an exception occurs&period; It is used in conjunction with a try block&period; The&nbsp&semi;<i>finally<&sol;i>&nbsp&semi;block contains statements that either return resources to the system or print messages&period;<&sol;p>&NewLine;<p><b>case 1&colon;<br &sol;>&NewLine;<i>Program in case exception does not occur<&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public class FinallyBlock&lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; int a&lbrack;&rsqb; &equals; new int&lbrack;&rsqb;&lbrace;2&comma;3&comma;5&rcub;&semi; &NewLine; try&lbrace; &NewLine; System&period;out&period;println&lpar;"Access element three &colon;" &plus; a&lbrack;2&rsqb;&rpar;&semi; &NewLine; &rcub;catch&lpar;ArrayIndexOutOfBoundsException e&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"Exception thrown &colon;" &plus; e&rpar;&semi; &NewLine; &rcub; &NewLine; finally&lbrace; &NewLine; a&lbrack;0&rsqb; &equals; 6&semi; &NewLine; System&period;out&period;println&lpar;"First element value&colon; " &plus;a&lbrack;0&rsqb;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"The finally statement is executed"&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>This would produce following result&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;finally1&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<b>case 2<&sol;b><br &sol;>&NewLine;<i><b>Program in case exception occurred and handled<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public class FinallyBlock&lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; int a&lbrack;&rsqb; &equals; new int&lbrack;&rsqb;&lbrace;2&comma;3&comma;5&rcub;&semi; &NewLine; try&lbrace; &NewLine; System&period;out&period;println&lpar;"Access element three &colon;" &plus; a&lbrack;3&rsqb;&rpar;&semi; &NewLine; &rcub;catch&lpar;ArrayIndexOutOfBoundsException e&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"Exception thrown &colon;" &plus; e&rpar;&semi; &NewLine; &rcub; &NewLine; finally&lbrace; &NewLine; a&lbrack;0&rsqb; &equals; 6&semi; &NewLine; System&period;out&period;println&lpar;"First element value&colon; " &plus;a&lbrack;0&rsqb;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"The finally statement is executed"&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>This would produce following result&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;finally2&period;png" &sol;><&sol;div>&NewLine;<p>&NewLine;<b>case 3<&sol;b><br &sol;>&NewLine;<i><b>Program in case exception occurred but not handled<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">public class FinallyBlock&lbrace; &NewLine; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; int a&lbrack;&rsqb; &equals; new int&lbrack;&rsqb;&lbrace;2&comma;3&comma;5&rcub;&semi; &NewLine; try&lbrace; &NewLine; System&period;out&period;println&lpar;"Access element three &colon;" &plus; a&lbrack;2&rsqb;&sol;0&rpar;&semi; &NewLine; &rcub;catch&lpar;ArrayIndexOutOfBoundsException e&rpar;&lbrace; &NewLine; System&period;out&period;println&lpar;"Exception thrown &colon;" &plus; e&rpar;&semi; &NewLine; &rcub; &NewLine; finally&lbrace; &NewLine; a&lbrack;0&rsqb; &equals; 6&semi; &NewLine; System&period;out&period;println&lpar;"First element value&colon; " &plus;a&lbrack;0&rsqb;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"The finally statement is executed"&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>This would produce following result&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;finally3&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<b>Note the following&colon;<&sol;b><&sol;p>&NewLine;<ul class&equals;"list">&NewLine;<li>A <i><b>catch <&sol;b><&sol;i>clause cannot exist without a <b><i>try <&sol;i><&sol;b>statement&period; <&sol;li>&NewLine;<li>It is not compulsory to have <i><b>finally <&sol;b><&sol;i>clauses when ever a <b><i>try&sol;catch<&sol;i><&sol;b> block is present&period; <&sol;li>&NewLine;<li>The <i><b>try <&sol;b><&sol;i>block cannot be present without either <i><b>catch <&sol;b><&sol;i>clause or <b><i>finally <&sol;i><&sol;b>clause&period;<&sol;li>&NewLine;<li>Any code cannot be present in between the <i><b>try&comma; catch&comma; finally<&sol;b><&sol;i> blocks&period;<&sol;li>&NewLine;<li>For each try block there can be zero or more catch blocks&comma; but only one finally block&period; <&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b><i>Note&colon; <&sol;i>The finally block will not be executed if program exits&lpar;either by calling System&period;exit&lpar;&rpar; or by causing a fatal error that causes the process to abort&rpar;&period; <&sol;b><&sol;p>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;nested-try-catch-block-handling&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-throw-and-throws-in&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;nested-try-catch-block-handling&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;difference-between-throw-and-throws-in&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '500'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago