例外処理を入れ子にしたときのサンプルを二つ程下に示す。
・サンプル1
【コーディング】
package sample3;
public class ExceptionSample3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main start.....");
try {
System.out.println("try1 start.....");
try {
System.out.println("try2 start.....");
int ikekka = 100/0;
System.out.println("try2 end.....");
} catch (ArithmeticException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("try1 end.....");
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("main end.....");
}
}
【実行結果】
main start.....
try1 start.....
try2 start.....
java.lang.ArithmeticException: / by zero
at sample3.ExceptionSample3.main(ExceptionSample3.java:15)
try1 end.....
main end.....
・サンプル2
【コーディング】
package sample3;
public class ExceptionSample3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main start.....");
try {
System.out.println("try1 start.....");
try {
System.out.println("try2 start.....");
int ikekka = 100/0;
System.out.println("try2 end.....");
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("try1 end.....");
} catch (ArithmeticException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println("main end.......");
}
}
【実行結果】
main start.....try1 start.....
try2 start.....
java.lang.ArithmeticException: / by zero
main end.......
at sample3.ExceptionSample3.main(ExceptionSample3.java:15)