반응형

*예외처리

1. try ~ catch

try {

}
catch(Exception e) {
}

 

2. throws 

- 방법1
public class ExceptionExam{
    public int get50thItem(int []array) throws ArrayIndexOutOfBoundsException {    
        return  array[49];
    }
}

- 방법2 
public class ExceptionExam{
    public int get50thItem(int []array){
        if(array.length < 50){
            throw new IllegalArgumentException();
        }
    return  array[49];
    }
}

 

3. 사용자 정의 Exception

- Checked exception 
Exception 클래스를 상속받은 경우 Checked exception이 됩니다. 
이 경우, 반드시 오류를 처리해야 하며 만약 예외처리 하지 않으면 컴파일 오류를 발생시킵니다. 

- Unchecked exception
RuntimeException을 상속받는 경우 Unchecked exception이 됩니다. 
이 경우에는 예외처리를 하지 않아도 컴파일시에 오류를 발생시키지 않습니다.
반응형

+ Recent posts