예외처리 Exception
package chap8;
// throws : 예외처리
// tyr mehod1() -> method1()에서 method2 호출 -> method2에서 catch 실행
// -> catch에 throw가 있으면 호출한 곳으로 알림 -> method1에서 catch 호출
// -> method1을 호출한 곳의 catch 호출. 로 흘러간다.
public class ExceptionEx7 {
public static void main(String[] args){
try{
method1();
} catch( Exception e ){ // throw로 넘어 왔다면 catch 실행한다.
System.out.println("main에서 예외처리");
}
}
private static void method1() throws Exception {
try{
method2();
} catch ( Exception e){
System.out.println("method1에서 예외처리");
throw e; // 2. 2에서 넘어온 에러 내용을 method1을 호출한 곳으로 간다.
}
}
private static void method2() throws Exception {
try{
throw new Exception("thorws로 예외처리해야함");
}catch ( Exception e ){
System.out.println("method2에서 예외처리");
throw e; // 1. 예외 재 발생 -> 호출한 곳으로 간다.
}
}
}