사용자정의 Exception
public class BizException extends RuntimeException {
public BizException(String msg){ // 문자열 형태로 메시지를 가져오는 생성자
super(msg);
}
public BizException(Exception ex){// exception을 받아 부모 생성자에게 넘겨주는 생성자
super(ex);// 부모의 생성자 호출
}
}
public class BizService {
public void bizMethod(int i)throws BizException{
System.out.println("비지니스 로직이 시작합니다.");
if(i < 0){
throw new BizException("매개변수 i는 0이상이어야 합니다.");
}
System.out.println("비지니스 로직이 종료됩니다.");
}
}
public class BizExam {
public static void main(String[] args) {
BizService biz = new BizService();
biz.bizMethod(5);// 메소드 실행
try{// 예외인 경우 exception
biz.bizMethod(-3);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
참고
--