본문 바로가기
IT

Business Exception vs System Exception

by urosie 2025. 12. 15.
반응형

1. 개요

예외를 구분하는 이유: 성격과 처리 방식이 다르기 때문이다.

  • Business Exception: 업무/사용자 관련 문제, 예측 가능
  • System Exception: 시스템/인프라 문제, 예측 불가능

2. Business Exception (업무 예외)

  • 사용자 또는 비즈니스 로직 문제
  • 예: 잔액 부족, 상품 없음, 권한 없음
  • 처리: 전역 핸들러에서 HTTP 400~409 수준 응답, 사용자 친화 메시지 제공
 
public class InsufficientBalanceException extends RuntimeException { public InsufficientBalanceException(String message) { super(message); } }

3. System Exception (시스템 예외)

  • 서버, DB, 네트워크, 외부 API 등 시스템 문제
  • 예: DB 커넥션 실패, NullPointerException
  • 처리: 전역 핸들러에서 HTTP 500 응답, 로그 기록 및 모니터링
 
try { externalApi.call(); } catch(IOException e) { throw new ExternalServiceException("외부 API 실패", e); }

4. 실무 처리 패턴

  1. 서비스 계층에서 업무 예외는 Business Exception으로 던짐
  2. 시스템 문제는 RuntimeException, System Exception으로 던짐
  3. @ControllerAdvice에서 예외 종류별 분기 처리
 
@ExceptionHandler(BusinessException.class) public ResponseEntity<String> handleBusiness(BusinessException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ex.getMessage()); } @ExceptionHandler(SystemException.class) public ResponseEntity<String> handleSystem(SystemException ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("서버 오류"); }

5. 한 줄 요약

Business Exception = 예측 가능한 업무 오류, System Exception = 예측 불가능한 시스템 오류 → 전역 핸들러에서 분리 처리하여 사용자 메시지와 HTTP 상태를 다르게 관리

반응형

댓글