<div dir="ltr" style="text-align: justify;" trbidi="on">The <i><b>finally </b></i>Keyword:<br />
The <b><i>finally </i></b>keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.</p>
<p> Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.</p>
<p> A finally block appears at the end of the catch blocks and has the following syntax:</p>
<pre class="highlight" name="code">try 
{ 
 //Protected code 
}catch(ExceptionType1 e1) 
{ 
 //Catch block 
}catch(ExceptionType2 e2) 
{ 
 //Catch block 
}catch(ExceptionType3 e3) 
{ 
 //Catch block 
}finally 
{ 
 //The finally block always executes. 
} 
</pre>
<div align='center' id="ads-id"></div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/finally.jpg"/></div>
<p><b><br />
<i>Note:</i> Before terminating the program, <a href="https://dineshonjava.com/2013/02/java-virtual-machine.html">JVM</a> executes <i>finally </i>block(if any).</b><br />
<b><br />
</b> <b><i>Note: finally </i>must be followed by <i>try </i>or <i>catch </i>block.</b><br />
<b><br />
</b> <b>Why use finally block?</b><br />
Sometimes there may be cleanup codes, which are required to be executed. However, when an execution occurs, the program halts abruptly and the clean up code never gets executed. To close a file before terminating a program, that has encountered an exception during execution, Java provides the ;<i>finally</i> ;block. The ;<i>finally</i> ;gets executed at runtime regardless of what happens in the ;<i>try/catch</i> ;block. A ;<i>finally</i> ;block ensures that all cleanup work is taken care of when an exception occurs. It is used in conjunction with a try block. The ;<i>finally</i> ;block contains statements that either return resources to the system or print messages.</p>
<p><b>case 1:<br />
<i>Program in case exception does not occur</i></b></p>
<pre class="highlight" name="code">public class FinallyBlock{ 
 
 public static void main(String args[]){ 
 int a[] = new int[]{2,3,5}; 
 try{ 
 System.out.println("Access element three :" + a[2]); 
 }catch(ArrayIndexOutOfBoundsException e){ 
 System.out.println("Exception thrown :" + e); 
 } 
 finally{ 
 a[0] = 6; 
 System.out.println("First element value: " +a[0]); 
 System.out.println("The finally statement is executed"); 
 } 
 } 
} 
</pre>
<p><b>This would produce following result:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/finally1.png"/></div>
<p>
<b>case 2</b><br />
<i><b>Program in case exception occurred and handled</b></i></p>
<pre class="highlight" name="code">public class FinallyBlock{ 
 
 public static void main(String args[]){ 
 int a[] = new int[]{2,3,5}; 
 try{ 
 System.out.println("Access element three :" + a[3]); 
 }catch(ArrayIndexOutOfBoundsException e){ 
 System.out.println("Exception thrown :" + e); 
 } 
 finally{ 
 a[0] = 6; 
 System.out.println("First element value: " +a[0]); 
 System.out.println("The finally statement is executed"); 
 } 
 } 
} 
</pre>
<p><b>This would produce following result:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/finally2.png" /></div>
<p>
<b>case 3</b><br />
<i><b>Program in case exception occurred but not handled</b></i></p>
<pre class="highlight" name="code">public class FinallyBlock{ 
 
 public static void main(String args[]){ 
 int a[] = new int[]{2,3,5}; 
 try{ 
 System.out.println("Access element three :" + a[2]/0); 
 }catch(ArrayIndexOutOfBoundsException e){ 
 System.out.println("Exception thrown :" + e); 
 } 
 finally{ 
 a[0] = 6; 
 System.out.println("First element value: " +a[0]); 
 System.out.println("The finally statement is executed"); 
 } 
 } 
} 
</pre>
<p>
<b>This would produce following result:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/finally3.png"/></div>
<p>
<b>Note the following:</b></p>
<ul class="list">
<li>A <i><b>catch </b></i>clause cannot exist without a <b><i>try </i></b>statement. </li>
<li>It is not compulsory to have <i><b>finally </b></i>clauses when ever a <b><i>try/catch</i></b> block is present. </li>
<li>The <i><b>try </b></i>block cannot be present without either <i><b>catch </b></i>clause or <b><i>finally </i></b>clause.</li>
<li>Any code cannot be present in between the <i><b>try, catch, finally</b></i> blocks.</li>
<li>For each try block there can be zero or more catch blocks, but only one finally block. </li>
</ul>
<p><b><i>Note: </i>The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort). </b></p>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/nested-try-catch-block-handling/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/difference-between-throw-and-throws-in/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/nested-try-catch-block-handling/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/difference-between-throw-and-throws-in/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '500'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…