-
[java] 익셉션 타입IT/Spring-JSP&Servlet 2020. 10. 28. 03:54
자바에서 예외 처리를 위해 단순히 try ~ catch를 사용했는데,
jsp/servlet 스터디 시 "익셉션 타입"에 따라 web.xml에 등록 가능하다는 내용을 보고 좀 더 공부하고자 정리해본다.
(출처: www.w3resource.com/java-tutorial/types-of-exception.php)
1. 자바에서는 익셉션을 객체로 표현하고, 익셉션/에러가 발생하는 시점에 객체가 생성된다.
아래는 자바 익셉션 객체의 구조이다.
출처 : w3resource.com 2. 자바에서 모든 익셉션 타입은 Throwable 클래스(java.lang.Throwable)의 서브 클래스이다.
Throwable클래스는 2개의 서브 클래스를 가지며 각각 Error 클래스와 Exception 클래스이다.
3. Exception 클래스
- 일반적으로 개발자(user)가 예상가능한 예외 조건을 잡아내기 위해 사용하며
- 개발자(user)가 직접 custom exception을 만들 수 있다. (custom exception 언제 쓸까?)
4. Error 클래스
- 프로그램 상 일반적인 환경에서 예상치 못한 예외를 처리하기 위해 사용
- 런타임 환경에서 사용됨
5. 자바 익셉션은 2가지 유형으로 구분
- Checked exception
- Unchecked exception
6. Checked exception
- 프로그래머가 예상가능하고 처리 가능한 예외
- 일반적으로 코드 외부 요인으로 발생
이름 설명 IOException 파일 input/output 스트림에 관련된 예외 SQLException SQL 구문 실행 시 발생하는 예외 DataAccessException 데이터 혹은 DB에 접근 시 발생하는 예외 ClassNotFoundException JVM이 클래스를 찾지 못해 발생하는 예외 - classpath 이슈, .class 파일 못 찾는 등등 InstantiationException abstract 클래스를 생성하지 못하는 경우 발생하는 예외 7. Unchecked exception
- Error 클래스 혹은 RuntimeException 클래스를 상속
- 일반적으로 잘못된 코딩으로 발생
이름 설명 NullPointerException Null 값을 사용하려고 하는 경우 ArrayIndexOutOfBound 일반적으로 배열 크기를 벗어난 인덱스 접근 시 발생 IllegalArgumentException Thrown when a method receives an argument formatted differently than the method expects. IllegalStateException Thrown when the state of the environment doesn’t match the operation being attempted,e.g., using a Scanner that’s been closed. NumberFormatException String을 number로 컨버팅 시 발생 (ex. %d에 String 대입) ArithmaticException 수학 연산 에러 (ex. divide-by-zero) 출처 : protechtraining.com ※ 참고 : www.protechtraining.com/content/java_fundamentals_tutorial-exceptions
'IT > Spring-JSP&Servlet' 카테고리의 다른 글
[JSP/Servlet] 익스프레션 언어(EL식) (0) 2020.11.01 [JSP/Servlet] 서블릿 라이프 사이클 - init(), service(), doGet(), destroy() (1) 2020.10.29 [JSP/Servlet] 익셉션 처리(2/2) - web.xml 파일 처리 (0) 2020.10.28 [JSP/Servlet] 익셉션 처리(1/2) - 에러 페이지 처리, JSP/Servlet에서 에러 페이지 호출 (0) 2020.10.28 웹 애플리케이션 - 서블릿& 톰캣 (0) 2020.10.12