Nested try catch Block in Java

Let’s see, What is nested try catch block in Java. The try-catch blocks can be nested. The try-catch block within the another try block body is known as nested try catch block.


Nested try-catch block

Sometimes the situation may arise. some code part may cause more than one error. In such case, exception handler has to be nested. This can be handled using the nested try-catch block i.e a try block inside the another try block.


We can understand this using this simple example –

The output of the above code will be – java.lang.ArrayIndexOutOfBoundsException: 4
java.lang.ArithmeticException: / by zero


Selenium Testing Video Tutorial:


You noticed, We already have seen this example in the multi-catch Block in Java tutorial. Where we were not using the nested try catch blocks and we were getting only one kind of exception.


Here you can see, In the above-nested try block code, each and every exception is handled. In order to handle each exception, we need to use try block.

0 Comment

Leave a Reply