can exceptions be nested

Exceptions can be nested, that means a try block can exist within another try block. Also note that a try block can exist in a catch block.

Below is an example showing nesting of Java Exceptions.


public class SimpleException {

	public static void main(String[] args) {
	try{
	System.out.println("in try");
	
	int arr[] = new int[20];
	
	arr[40] = 23;
	System.out.println("in try 2");
	
	try{
	if(true)
	{
	throw new Exception("Sample Exception");
	}
	}
	catch(Exception en)
	{
	System.out.println("Nested try catch:"+en.getMessage());
	}
	System.out.println("end of outside try");
	}
	catch(ArithmeticException e)
	{
	System.out.println("AE occurred:"+e.getMessage());
	}
	catch(ArrayIndexOutOfBoundsException ae)
	{
	System.out.println("ABE occurred:"+ae.getMessage());
	}
	catch(Exception en)
	{
	en.printStackTrace();
	System.out.println("Exception occurred:"+en.getMessage());
	}
	}

}