非チェック例外発生時に例外処理した例をいくつか下に示す。
この結果から、例外が発生した場合、そのメソッド内でのcatch節で処理されるが、
catch節が見つからなかった場合、自分を呼び出したメソッドにさかのぼってcatch節を探す事が解る。
で、最後までcatch節が見つからなかった場合、システムがエラーメッセージを表示して、プログラムが終了する。
・サンプル1
【コーディング】
public class ExceptionSample2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main start.....");
try {
System.out.println("try-before");
int ikekka = 100/0;
System.out.println("try-end");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
System.out.println("main end.....");
}
}
【実行結果】
main start.....
try-before
java.lang.ArithmeticException: / by zero
main end.....
サンプル2
【コーディング】
package sample2;
public class ExceptionSample2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main start.....");
try {
System.out.println("try-before");
HogeClass classHoge = new HogeClass();
classHoge.hogeMethod();
System.out.println("try-end");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
System.out.println("main end.....");
}
}
package sample2;
public class HogeClass {
public static void hogeMethod(){
System.out.println("HogeClass.hogeMethod start.....");
int ikekka = 100/0;
System.out.println("HogeClass.hogeMethod end.....");
}
}
【実行結果】
main start.....
try-before
HogeClass.hogeMethod start.....
java.lang.ArithmeticException: / by zero
main end.....
・サンプル3
【コーディング】
package sample2;
public class ExceptionSample2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main start.....");
HogeClass classHoge = new HogeClass();
classHoge.hogeMethod();
System.out.println("main end.....");
}
}
package sample2;
public class HogeClass {
public static void hogeMethod(){
System.out.println("HogeClass.hogeMethod start.....");
try {
System.out.println("try start.....");
int ikekka = 100/0;
System.out.println("try end......");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
System.out.println("HogeClass.hogeMethod end.....");
}
}
【実行結果】
main start.....
HogeClass.hogeMethod start.....
try start.....
java.lang.ArithmeticException: / by zero
HogeClass.hogeMethod end.....
main end.....
サンプル4
【コーディング】
package sample2;
public class ExceptionSample2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("main start.....");
try {
System.out.println("main try start.....");
HogeClass classHoge = new HogeClass();
classHoge.hogeMethod();
System.out.println("main try end.....");
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("main end.....");
}
}
package sample2;
public class HogeClass {
public static void hogeMethod(){
System.out.println("HogeClass.hogeMethod start.....");
try {
System.out.println("hoge try start.....");
int ikekka = 100/0;
System.out.println("hoge try end......");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
System.out.println("HogeClass.hogeMethod end.....");
}
}
【実行結果】
main start.....
main try start.....
HogeClass.hogeMethod start.....
hoge try start.....
java.lang.ArithmeticException: / by zero
at sample2.HogeClass.hogeMethod(HogeClass.java:8)
at sample2.ExceptionSample2.main(ExceptionSample2.java:14)
HogeClass.hogeMethod end.....
main try end.....
main end.....