<div dir="ltr" style="text-align: justify;" trbidi="on">In Java we can have nested try and catch blocks. It means that, a try statement can be inside the block of another try. If an inner try statement does not have a matching catch statement for a particular exception, the control is transferred to the next try statement?s catch handlers that are expected for a matching catch statement. This continues until one of the catch statements succeeds, or until all of the nested try statements are done in. If no one catch statements match, then the Java run-time system will handle the exception. <br />
<b><br />
</b> <b>Why use nested try block?</b><br />
Sometimes a situation may arise where a part of a block may cause one error and the entire block itself may cause another error. In such cases, exception handlers have to be nested, one within another. In java, this can be done using<i>nested try</i> ;blocks. A ;<i>try</i> ;statement can be inside the block of another ;<i>try</i>. In a ;<i>nested try</i> ;block, every time a try block is entered the context of that exception is pushed on the stack.</p>
<div align='center' id="ads-id"></div>
<p>The following code snippet explains the syntax of a nested try…catch block. </p>
<p><b>Syntax:</b></p>
<pre class="highlight" name="code">.... 
try 
{ 
 ; ; ; statement 1; 
 ; ; ; statement 2; 
 ; ; ; try 
 ; ; ; { 
 ; ; ; ; ; ; ; statement 1; 
 ; ; ; ; ; ; ; statement 2; 
 ; ; ; } 
 ; ; ; catch(Exception e) 
 ; ; ; { 
 ; ; ; } 
} 
catch(Exception e) 
{ 
} 
....</pre>
<p>
When nested try blocks are used, the inner try block is executed first. Any exception thrown in the inner try block is caught in the corresponding catch block. If a matching catch block is not found, then catch block of the outer try block are inspected until all nested try statements are exhausted. If no matching blocks are found, the Java Runtime Environment handles the execution.</p>
<p> Lets have an example that uses the nested try-catch blocks.</p>
<pre class="highlight" name="code">import java.io.*; 
public class NestedTryCatchBlock{ 
 public static void main (String args[])throws IOException { 
 int num=2,res=0; 
 
 try{ 
 FileInputStream fis=null; 
 fis = new FileInputStream (new File (args[0])); 
 try{ 
 res=num/0; 
 System.out.println("The result is"+res); 
 } 
 catch(ArithmeticException e){ 
 System.out.println("divided by Zero"); 
 } 
 } 
 catch (FileNotFoundException e){ 
 System.out.println("File not found!"); 
 } 
 catch(ArrayIndexOutOfBoundsException e){ 
 System.out.println("Array index is Out of bound! Argument required"); 
 } 
 catch(Exception e){ 
 System.out.println("Error.."+e); 
 } 
} 
} 
</pre>
<p>
<b>Output of the program:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/nestedtry.png"/></div>
<p>
In this given example we have implemented nested try-catch blocks concept where an inner try block is kept with in an outer try block, that&#8217;s catch handler will handle the arithmetic exception. But before that an <b><i>ArrayIndexOutOfBoundsException </i></b>will be raised, if a file name is not passed as an argument while running the program. </p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/multiple-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/the-finally-keyword-finally-keyword-is/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/multiple-catch-block-handling/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/the-finally-keyword-finally-keyword-is/">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: '501'});
});
</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…