Exception

프로그램 실행 중 발생하는 예외 상황입니다.

예시

int result = 10 / 0;  // ArithmeticException: 0으로 나눌 수 없음

String str = null;
str.length();  // NullPointerException: null에 메서드 호출

int[] arr = new int[3];
arr[5] = 10;  // ArrayIndexOutOfBoundsException: 범위 초과

왜 필요한가

예외 처리가 없으면 프로그램이 그냥 죽습니다.

// 예외 처리 없음
int result = 10 / 0;
System.out.println("여기 실행 안 됨");  // 프로그램 종료됨
// 예외 처리 있음
try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("0으로 나눌 수 없습니다");
}
System.out.println("프로그램 계속 실행됨");  // 정상 실행

Error vs Exception

ErrorException
원인시스템 문제프로그램 로직 문제
복구불가능가능
예시OutOfMemoryError, StackOverflowErrorNullPointerException, IOException

Error는 메모리 부족 같은 심각한 문제라 처리해봤자 의미 없고, Exception은 개발자가 처리할 수 있는 예외 상황입니다.

Checked vs Unchecked 예외

Throwable
├── Error           (시스템 수준 — 처리 불필요)
└── Exception
    ├── RuntimeException          ← Unchecked
    │    ├── NullPointerException
    │    ├── IllegalArgumentException
    │    ├── IndexOutOfBoundsException
    │    └── ClassCastException
    └── (그 외 Exception)         ← Checked
         ├── IOException
         ├── SQLException
         └── FileNotFoundException
CheckedUnchecked
상속Exception (RuntimeException 제외)RuntimeException
컴파일 강제try-catch 또는 throws 선언 필수선택
발생 원인외부 환경 (파일, DB, 네트워크)프로그래밍 오류 (null, 잘못된 인덱스)
예시IOException, SQLExceptionNullPointerException, IllegalArgumentException
// Checked — 컴파일러가 처리 강제
void readFile(String path) throws IOException {  // throws 선언 필수
    BufferedReader br = new BufferedReader(new FileReader(path));
}

// 또는 try-catch로 처리
void readFile(String path) {
    try {
        BufferedReader br = new BufferedReader(new FileReader(path));
    } catch (IOException e) {
        // 처리
    }
}

// Unchecked — 처리 선택
void divide(int a, int b) {
    if (b == 0) throw new IllegalArgumentException("0으로 나눌 수 없음");
    return a / b;
}

try-with-resources (Java 7+)

AutoCloseable을 구현한 자원을 자동으로 닫아줌.

// X 기존 방식 — finally에서 close() 직접 호출
BufferedReader br = null;
try {
    br = new BufferedReader(new FileReader("file.txt"));
    return br.readLine();
} finally {
    if (br != null) br.close();
}

// O try-with-resources
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    return br.readLine();
}  // 블록 종료 시 자동으로 br.close() 호출

커스텀 예외

// Unchecked 커스텀 예외 (실무에서 더 선호)
public class PostNotFoundException extends RuntimeException {
    public PostNotFoundException(Long id) {
        super("Post not found: " + id);
    }
}

// 사용
throw new PostNotFoundException(id);

핵심 정리

→ 어떤 예외가 발생하면 트랜잭션을 롤백하나요?

CSLOG에서 보기