728x90
Exception을 Custom 하여 명확한 에러를 처리해보자
1. 원하는 Exception class 생성하여 'RuntimeException'을 상속하면 됨
public class NotEnoughStockException extends RuntimeException {
public NotEnoughStockException() {
}
public NotEnoughStockException(String message) {
super(message);
}
public NotEnoughStockException(String message, Throwable cause) {
super(message, cause);
}
public NotEnoughStockException(Throwable cause) {
super(cause);
}
}
2. throw 하는 법
(...생략)
public void removeStock(int quantity) {
int restStock = this.stockQuantity - quantity;
if (restStock < 0) {
// >>>>>>>>>>>> 여기
throw new NotEnoughStockException("need more stock");
}
this.stockQuantity = restStock;
}
}
(...생략)
'서버' 카테고리의 다른 글
Springboot3 - 문자인증 구현(coolsms) (1) | 2025.01.03 |
---|---|
vscode 에서 java 버전 변경(java extension 설치 후 기본 버전(jdk21)으로 셋팅되는 경우) (0) | 2025.01.02 |
Springboot3 - 테스트환경 메모리 모드로 설정 (0) | 2024.12.30 |
Springboot3 - SpringBootTest에서 insert문 안 나가는 이유(@Transactional, @Rollback(false) (0) | 2024.12.30 |
Springboot - vscode에서 hot swapping 셋팅 (1) | 2024.12.26 |