- 개발자가 직접 예외 클래스를 정의할 수 있다. - 조상은 Exception과 RuntimeException중에서 선택
public class CustomException extends Exception {
// 1. 기본 생성자
public CustomException() {
super();
}
// 2. 메시지를 받는 생성자
public CustomException(String message) {
super(message);
}
}
ch8-17 예외 되던지기(Exception re-throwing)
- 예외를 처리한 후에 다시 예외를 발생시키는 것 - 호출한 메서드와 호출된 메서드 양쪽에서 처리하는 것
public class Ex8_12 {
public static void main(String[] args) {
try{
method1();
} catch (Exception e){
System.out.println("main 메서드에서 예외가 처리되었습니다.");
}
} // main 메서드
static void method1() throws Exception{
try{
throw new Exception();
} catch (Exception e) {
System.out.println("method1에서 예외를 처리하였습니다.");
throw e; // 다시 예외를 발생시킨다.
}
} // method1() 끝
}