JAVA/예제
[JAVA] ch08-15. 예외 처리 15
밍글링글링
2017. 8. 22. 11:54
728x90
public class Ex15 {
public static void main(String[] args) {
method1();
}
static void method1(){
try{
method2();//throw new Exception(); Exception은 try catch 구문 안에서 작동해야 하므로 try catch 구문을 써줘야 한다.
}catch(Exception e){}
method3();//throw new RuntimeException(); RuntimeException은 try catch 구문이 필요없으므로 compile error가 안남.
}
static void method2() throws Exception{
throw new Exception();
}
static void method3() throws RuntimeException{//throws를 안 쓰게 되면 method1()에서 method3()을 실행하면 애플리케이션이 종료 된다.
throw new RuntimeException(); //하지만 throws를 쓰게 되면 예외처리 할 기회를 한번 주게 된다.
} //throws를 쓸 경우, try{ method3(); } catch(RuntimeException re){}
} //throws를 안 쓸 경우, method3() = throw new RuntimeException(); 애플리케이션 종료
//throws: 해당 타입의 Exception 객체가 발생하면 호출한 쪽 Exception 객체를 호출한 쪽으로 떠넘겨버린다.
728x90