- 시스템 동작 도중 예상치 못한 상태가 발생하여 프로그램이 영향을 받는 오류
- Error와는 다른데, Error는 컴파일 중에 인지할수 있는 오류이지만 Exception은 예상치 못한 상태에서 발생하기에 인지하기 어렵다
- 자바 특성상 Exception들이 클래스로 이루어져 있고 하위에도 여러 클래스들이 존재한다
- Exception은 크게 Runtime이냐 아니냐에 따라 나눌수 있는데
종류
RuntimeException
- 주로 개발자 실수로 인해 발생되는 예외들이라 생각하면 됨
- 이런 예외들은 try-catch로 잡기보다는 소스를 고쳐 발생하지 않도록 하는것이 좋음
- 주로 어떤 것들이 있냐면
- 배열의 범위를 벗어난 IndexOutOfBoundExcetion
- null 참조를 호출하려 한 NullPointerExcetpion
- 클래스 형변환이 잘못된 ClassCastExcetion
- 정수를 0으로 나누려 한 ArithmeticExcetion
그 외 Exception
- 외부 영향을 받을수 있는 예외들, 유저에 의해 발생하는 경우가 많다
- 예를 들자면
- 존재하지 않는 파일 처리를 하려한 FileNotFoundException
- 데이터 형식이 잘못된 DataFormatExcetion
- 이 예외들은 try-catch로 처리해주는 것이 좋음