new
로 발생시키려는 예외 객체 생성Exception e = new Exception("에러 메세지");
"에러 메세지"
==getMessage()
throw
로 예외 발생시키기throw e;
->
try { Exception e = new Exception("에러 메세지"); throw e; } catch (Exception e) {}
-> 한 줄로
try { throw new Exception("에러 메세지"); } catch (Exception e) {}
public class Ex8_06 {
public static void main(String[] args) {
try {
Exception e = new Exception(); //예외 객체 생성
throw e; //참조변수 던져서
// throw new Exception(); //한줄로 줄이면
} catch (Exception e) { //얘가 참조변수 잡기
System.out.println("예외 메세지 : "+e.getMessage());
//참조변수의 기본값은 null
e.printStackTrace();
}
System.out.println("프로그램이 정상종료되었음.");
}
}
try-catch
안하면 컴파일 에러Exception
클래스들try-catch
안해도 컴파일 OKRuntimeException
클래스들