0% found this document useful (0 votes)
32 views3 pages

Why Use Nested Try Block

A nested try block in Java allows try blocks to be nested within other try blocks. This is useful when a section of code may cause one type of error while the overall block causes another type of error. The syntax includes try blocks within other try blocks, with inner catch blocks to handle specific exceptions and an outer catch block to handle general exceptions. The example code shows a main method with an outer try block containing two inner try blocks, each with their own catch blocks to handle specific exceptions, with an outer catch block to handle general exceptions.

Uploaded by

shivaji boss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Why Use Nested Try Block

A nested try block in Java allows try blocks to be nested within other try blocks. This is useful when a section of code may cause one type of error while the overall block causes another type of error. The syntax includes try blocks within other try blocks, with inner catch blocks to handle specific exceptions and an outer catch block to handle general exceptions. The example code shows a main method with an outer try block containing two inner try blocks, each with their own catch blocks to handle specific exceptions, with an outer catch block to handle general exceptions.

Uploaded by

shivaji boss
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Nested try block

The try block within a try block is known as nested try block in java.

Why use nested try block

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.

Syntax:

....

try

statement 1;

statement 2;

try

statement 1;

statement 2;

catch(Exception e)

catch(Exception e)
{

....

Java nested try example


Let's see a simple example of java nested try block.

class Excep6{

public static void main(String args[]){

try{

try{

System.out.println("going to divide");

int b =39/0;

}catch(ArithmeticException e){System.out.println(e);}

try{

int a[]=new int[5];

a[5]=4;

}catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}

System.out.println("other statement);

}catch(Exception e){System.out.println("handeled");}

System.out.println("normal flow..");

}
}

prev next

Please Share

Latest 4 Tutorials

PouchDB DB2 MariaDB

ADO.NET

You might also like