1.

What Is A Nested Try Statement?

Answer»

A try-catch-finally block can reside inside another try-catch-finally block that is known as nested try statement.

PUBLIC class NestedTryDemo {
public STATIC void main(String[] args) {
try{
System.out.println("In Outer try block");
try{
System.out.println("In Inner try block");
int a = 7 / 0;
}catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException CAUGHT");
}finally{
System.out.println("In Inner finally");
}
}catch (ArithmeticException e) {
System.out.println("ArithmeticException caught");
}finally {
System.out.println("In Outer finally");
}
}
}

A try-catch-finally block can reside inside another try-catch-finally block that is known as nested try statement.

public class NestedTryDemo {
public static void main(String[] args) {
try{
System.out.println("In Outer try block");
try{
System.out.println("In Inner try block");
int a = 7 / 0;
}catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException caught");
}finally{
System.out.println("In Inner finally");
}
}catch (ArithmeticException e) {
System.out.println("ArithmeticException caught");
}finally {
System.out.println("In Outer finally");
}
}
}



Discussion

No Comment Found