
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Nested Try-Catch Blocks in Java
Yes, we can declare a try-catch block within another try-catch block in Java, this is called nested try-catch block. The try-catch block is used to handle runtime errors that occur during the execution of a program, and the process of handling these errors is called exception handling.
The runtime errors or exceptions must be handled in order to maintain the normal flow ofaJava program.
Try-Catch Block in Java
In a try-catch block, the code that might throw an exception should be placed or written within a try block. The catch block is used to handle the exception that is thrown from the try block. When an exception occurs in the try block, the control is transferred to the associated catch block.
Syntax of Try-Catch
The syntax for the Try-Catch block is as shown below:
try { // code that may throw exception } catch(ExceptionType1 e1) { // catch the corresponding ExceptionType1 } catch(ExceptionType2 e2) { // catch the corresponding ExceptionType2 }
Example
Let's see an example of a try-catch block in Java:
public class Example { public static void main(String[] args) { try { int a = 10/0; } catch(ArithmeticException e) { System.out.println("Arithmetic Exception: cannot be divided by zero"); } } }
Output:
Arithmetic Exception: cannot be divided by zero
Nested Try-Catch Block
Inside a nested try-catch block, the inner try block is executed first. Any exception thrown in the inner try block is caught in the corresponding catch block.
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 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.
If no matching blocks are found, the Java Runtime Environment handles the execution.
Nested Try-Catch Syntax
This is a syntax for a nested try-catch block in Java:
try { statement 1; statement 2; try { statement 1; statement 2; } catch(Exception e) { // catch the corresponding exception } } catch(Exception e) { // catch the corresponding exception }
Example
A Java program that shows how to declare a try-catch block within another try-catch block is given below:
import java.io.*; public class NestedTryCatchTest { public static void main (String args[]) throws IOException { int n = 10, result = 0; // outer try block try { FileInputStream fis = null; fis = new FileInputStream (new File (args[0])); // inner try block try { result = n/0; System.out.println("The result is"+result); } // inner catch block catch(ArithmeticException e) { System.out.println("Division by Zero"); } } // outer catch block catch (FileNotFoundException e) { System.out.println("File was not found"); } // outer catch block catch(ArrayIndexOutOfBoundsException e) { System.out.println("Array Index Out Of Bounds Exception occured "); } // outer catch block catch(Exception e) { System.out.println("Exception occured"+e); } } }
Output:
Array Index Out Of Bounds Exception occured