You can learn about Multiple Try Blocks in Java with Example.In this tutorial you can find the concept of Multiple try blocks with sample program.
public class MultipleTryStmtDemo {
/**
* @param args
*/
public static void main(String[] args) {
int ary[]={1,2,3};
try {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter number”);
int b = Integer.parseInt(br.readLine());
int c=2/b;
System.out.println(c);
try {
if(b == 1){
c= 2/(b-1);
}
if(b == 2){
System.out.println(ary[10]);
}
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e);
}
}catch (ArithmeticException e) {
System.out.println(e);
}catch (NumberFormatException e) {
System.out.println(e);
}catch (IOException e) {
System.out.println(e);
}
}
}
Result:
i)
Enter number
0
java.lang.ArithmeticException: / by zero
ii)
Enter number
1
2
java.lang.ArithmeticException: / by zero
iii)
Enter number
2
1
java.lang.ArrayIndexOutOfBoundsException: 10