0) docs 가이드
You should write your own exception classes if you answer yes to any of the following questions; otherwise, you can probably use someone else's.
1.Do you need an exception type that isn't represented by those in the Java platform?
2.Would it help users if they could differentiate your exceptions from those thrown by classes written by other vendors?
3.Does your code throw more than one related exception?
4.If you use someone else's exceptions, will users have access to those exceptions? A similar question is, should your package be independent and self-contained?
https://docs.oracle.com/javase/tutorial/essential/exceptions/creating.html
1) 여러 타입의 exception이 발생할 수 있는 코드에서 한가지 Exception으로 묶어 처리 할 때
2) 상세한 예외 정보 제공해야 할 때
To catch and provide specific treatment to a subset of existing Java exceptions
If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way.
One case where it is common practice to throw a RuntimeException is when the user calls a method incorrectly. For example, a method can check if one of its arguments is incorrectly null. If an argument is null, the method might throw a NullPointerException, which is an unchecked exception.
네이밍 컨벤션을 따르자
예외에는 주석을 달아주자
예외 만드는 방법
주의 : error.html 로 인한 삽질
At start-up, Spring Boot tries to find a mapping for /error. By convention, a URL ending in /error maps to a logical view of the same name: error. In the demo application this view maps in turn to the error.html Thymeleaf template. (If using JSP, it would map to error.jsp according to the setup of your InternalResourceViewResolver). The actual mapping will depend on what ViewResolver (if any) that you or Spring Boot has setup.
출처 https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
프로그램을 작성한다는 건 한편의 플로우 차트를 짜는 것과 같습니다.
제 생각입니다만 내가 머릿속에 그린 플로우 차트의 가지에 담긴 상황이라면, 예외는 아닙니다.가위바위보를 할 때, 가위-바위-보 가 나오는 경우에 대해서는 예외처리를 안 하는 것처럼
package com.team19.airbnb.exception;
import com.team19.airbnb.ResponseBody;
import com.team19.airbnb.exception.notauthorized.NotAuthorizedException;
import com.team19.airbnb.exception.notfound.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class AirbnbExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(AirbnbExceptionHandler.class);
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseBody<String> handleNotFoundException(NotFoundException e) {
logger.error(e.getMessage());
return ResponseBody.notFound(e.getMessage());
}
@ExceptionHandler(NotAuthorizedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseBody<String> handleNotAuthorizedException(NotAuthorizedException e) {
logger.error(e.getMessage());
return ResponseBody.notFound(e.getMessage());
}
}
100-level (Informational) — Server acknowledges a request
200-level (Success) — Server completed the request as expected
300-level (Redirection) — Client needs to perform further actions to complete the request
400-level (Client error) — Client sent an invalid request
500-level (Server error) — Server failed to fulfill a valid request due to an error with server
산술적인 연산에 오류가 있을 때
위 링크된 블로그에서는 많이 사용되는 5가지 예외에 ConcurrentModificationException, UnsupportedOperationException, NumberFormatException 가 작성되어있다.