반응형
*예외처리
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이 됩니다.
이 경우에는 예외처리를 하지 않아도 컴파일시에 오류를 발생시키지 않습니다.
반응형
'알고리즘 > 프로그래머스, 백준, 구름' 카테고리의 다른 글
스트링 버퍼(StringBuffer) (0) | 2020.06.03 |
---|---|
래퍼 클래스(wrapper class), 오토 박싱, 오토 언박싱 (0) | 2020.06.03 |
인터페이스 (0) | 2020.06.01 |
ArrayList (import, 객체 생성, 추가/삭제, 엘리먼트 가져오기, iterator 등) (0) | 2020.06.01 |
삼항연산자, switch문, for each문, 문자열 붙이기, 문자열 자르기 (0) | 2020.06.01 |