현재 상황
extract 함수에 throws(예외 던지기)가 설정 안되어 있을 때,
상위 호출자에서 수신되는 과정에서 문제가 발생.
원인
@controlleradvice(글로벌 예외처리 어노테이션)에 의해 예외처리가 해당 클래스에서 처리됨
public List<TestVo> extract(TestVo testVo) {
try {
주요로직 //그러나 StrangeError가 발생
} catch (Exception e) {
throw e;
}
}
상위 호출자:
try {
testService.extract(testVo);
} catch (Exception e) {
// 예외가 상위 호출자의 catch로 전달되어 처리됨
}
(RuntimeException의 하위 클래스인) StrangeError라 명시한 에러가 발생한 경우
<EX 1>
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleAllExceptions(Exception ex) {
return new ResponseEntity<>("An error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
상위 호출자에서 처리 됨.
<EX 2>
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(StrangeError.class)
public ResponseEntity<String> handleAllExceptions(StrangeError ex) {
return new ResponseEntity<>("An error occurred: " + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
글로벌 예외 처리됨
우와 딱봐도 어려워보여요 짱이예요 최고다