Java Nested Try Block Example
Java allows the use of try blocks inside of other try blocks. The term “nested try block” describes it. The context of the exception is pushed onto the stack with each sentence we enter in a try block.
For instance, the outside try block can handle the ArithemeticException (division by zero), whereas the inside try block can handle the ArrayIndexOutOfBoundsException.
Use of Nested
Sometimes a block contains a section that leads to one error while the entire block itself leads to another error. It is necessary to nest exception handlers in such situations.
Syntax:
Try in the //main try block. declaration 1; phrasing 2; //inside another try block, a try catch block try { sentence 3; sentence 4; //nested try block inside a try catch block try { sentence 5; sentence 6; } Exception e2: catch { main try block //exception message try { declaration 1; phrasing 2; //inside another try block, a try catch block try { sentence 3; sentence 4; //nested try block inside a try catch block try { sentence 5; sentence 6; } Exception e2: catch { \\ the exception message } } catch(Exception e1) { //exception message } } //catch block of parent (outer) try block catch(Exception e3) { //exception message }
Nested try block in Exception handling:
We can use a try block inside another try block in Java. The context of that exception is pushed onto a stack each time a try statement is entered.
An example of a nested try is provided below. In this example, the ArithmeticException—that is, division by zero—is handled by the inner try block (also known as try-block 2).
The ArrayIndexOutOfBoundsException is then handled by the outer try block, also known as the try-block.
In the event that a try block lacks a catch block for a specific exception, the parent try block’s catch blocks are searched for that exception; if a match is found, the catch block is then executed.
A system-generated message will be displayed for the exception and the Java run-time system will handle it if none of the catch blocks handle it.
Example:
class FirstCodeNestedTry { // main method public static void main(String args[]) { // Main try block try { // initializing array int a[] = { 1, 2, 3, 4, 5 }; System.out.println(a[6]); // try-block2 inside another try block try { // performing division by zero int x = a[2] / 0; } catch (ArithmeticException e2) { System.out.println("division by zero is not possible"); } } catch (ArrayIndexOutOfBoundsException e1) { System.out.println("ArrayIndexOutOfBound"); System.out.println("Element does not exists"); } } // end of main method }
Output:
ArrayIndexOutOfBound
Element does not exist
Example
public class FirstCode{ public static void main(String args[]){ try{ try{ int b =9/0; } catch(ArithmeticException e) { System.out.println(e); } try{ int a[]=new int[5]; a[5]=0; } catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); } System.out.println("Welcome to FirstCode Java Tutorial"); } catch(Exception e) { System.out.println("Exception catched in outer catch statement in FirstCode"); } System.out.println("Continue learning in FirstCode"); } }
Output
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
Welcome to FirstCode Java Tutorial
Continue learning in FirstCode
No command line: If you run the program without any command-line arguments, the outer try block will throw a divide-by-zero exception.
One command-line parameter: Running the program with just one command-line argument causes the nested try block to throw a divide-by-zero exception. This exception is transferred to the outer try block for handling because the inner block did not capture it.
Two command-line arguments: If you run the program with two command-line arguments, the inner try block throws an array boundary exception. Here are several examples that represent each situation:
Notes
The control is passed to the subsequent try statement catch handlers that are anticipated for a matching catch statement if an inner try statement does not contain a matching catch statement for a specific exception.
This keeps on until either one of the catch statements is successful or all of the nested try statements have been completed.
The Java run-time system will handle the exception in the absence of any matching catch statements.
When a try block doesn’t include a catch handler for a certain exception, the catch blocks of the parent try block are checked to see whether they match, and if they do, that catch block is put into action.
The run-time system generated message would be if neither the catch block nor the parent catch block handles the exception.
Conclusion
nested try-catch blocks in Java allow you to manage exceptions in a more fine-grained manner. They enable the use of an inner try-catch block within an outside try-catch block. When an exception occurs within the inner try block, the program can choose to capture and manage it locally, or it can propagate to the outer try block for further processing if it is not caught within the inner block.