Title: Nest try blocks.
Course: Java Programming (Oracle) (10055) > Section: Handling Exceptions

Nesting try blocks should be

- performed when an exception generated within the inner try block that is not caught by a catch is propagated to the outer try block
- continued up the stack trace until the exception is caught by the appropriate handler.

An example of a nested try block follows:

//Use a nested try block

class NestTrys {
public static void main (String args[ ])  {
//Here, numer is longer than denom

int numer[] = {4, 8, 16, 32, 64,  128, 256, 512};
int denom[] = {2, 0, 4, 4, 0, 8};

try {
//outer try block
for(int i = 0;  i < numer.length;  i++)  {

try{
//the inner try block
System.out.println(numer[i] + "/" +
denom[i] + " is " +
numer[i] /denom[i]);

}
catch (ArithmeticException exc)  {
//some code to tell the user what happened to cause the error
System.out.println("Can’t divide by Zero!");
}
}
}
catch (ArrayIndexOutOfBoundsException exc) {
the exception if the program tries to access an array index that doesn’t exist
//the code is specifically tailored to match the purpose of the program

System.out.println("No matching element found.");
System.out.println("Fatal error - - program terminated.");
}
}
}