JAVA 예외처리2 throws


throws Exception은 에러가 날경우 호출했던 위치로 리턴 시킨다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class MyClass{
    
    int[] ary = {1,2,3};
    
    public static void main(String args[]){
        new MyClass().A();
    }    
 
    void A(){
        try{
            System.out.println("!!!!!");
            B();
        }catch(Exception e){
            System.out.println("오류 발생"); // B()에서 오류 발생
            System.out.println(e.getMessage());
        }
        System.out.println("$$$$$");
    }
 
    void B() throws Exception{
        System.out.println("#####");
        System.out.println(ary[3]); // 오류 발생 배열 A()에 리턴
        System.out.println("#####"); // 실행안됨
    }
}
cs
result

!!!!!
#####
오류 발생
3
$$$$$

댓글