less than 1 minute read

8.1 (사용자 정의 예외, 연결된 예외)

사용자 정의 예외 만들기

  • Exception, RuntimeException 클래스 상속으로 예외 클래스를 만듬
class MyException extends Exception {
	private final int ERR_CODE; //생성자로 초기화

	MyException(String msg, int errCode) {
		super(msg); //조상 Exception 클래스 생성자 호출
		ERR_CODE = errCode

	public int getErrCode() { //ERR_CODE get method
			return ERR_CODE;
		}
	}
}
  • Exception 상속
    • checked 예외
    • 예외처리 필수
  • RuntimeException 상속
    • unchecked 예외
    • 예외처리 선택

연결된 예외

try {
	startInstall();
	copyFIles();
} catch (SpaceException e)
	InstallException ie = new InstallException("설치중 예외발생");
	ie.inintCause(e); //ie의 원인 예외를 e로 등록, ie.getCause()로 원인 예외 반환
	throw ie;         //ie 예외 던지기

Leave a comment