throws節のサンプルを一つ。


実行結果から、hogeMethod()で発生したArithmeticException例外をExceptionとしてthrowsで投げた場合でも


呼び出し元でArithmeticException例外だと判断してくれいることがわかる。


【コーディング】

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 (ArithmeticException e2){
System.out.println("ArithmeticException.....");
System.out.println(e2);
} catch (Exception e) {
// TODO: handle exception
System.out.println("Exception.....");
System.out.println(e);
}
System.out.println("main end.....");
}
}

package sample2;

public class HogeClass {
public static void hogeMethod() throws Exception{
System.out.println("HogeClass.hogeMethod start.....");
int ikekka = 100/0;
System.out.println("HogeClass.hogeMethod end.....");
}
}

【実行結果】

main start.....
main try start.....
HogeClass.hogeMethod start.....
ArithmeticException.....
java.lang.ArithmeticException: / by zero
main end.....