Throwable vs Exception — 예외 계층 구조와 catch 대상 고르기
try-catch를 사용하다 보면 catch (Exception e)는 자주 보는데, 어떤 코드에서는 catch (Throwable t)가 등장합니다. 둘은 비슷해 보이지만 처리하는 범위가 다르고, 잘못 고르면 처리하지 말아야 할 문제까지 묻어 버립니다. 차이를 이해하는 핵심은 예외 계층 구조 — Throwable이 최상위이고, 그 아래 Error와 Exception이 성격이 전혀 다른 두 부류로 나뉜다는 점입니다.
계층 구조 한눈에
Java에서 throw로 던지고 catch로 처리할 수 있는 모든 것은 Throwable을 상속합니다. 그 아래로 크게 두 가지로 나뉩니다 — Error와 Exception.
SVG 코드
<svg xmlns="http://www.w3.org/2000/svg" width="440" height="360" viewBox="0 0 440 360" font-family="-apple-system, 'Segoe UI', 'Noto Sans KR', sans-serif">
<defs><marker id="tri" viewBox="0 0 12 12" refX="11" refY="6" markerWidth="11" markerHeight="11" orient="auto"><path d="M1,1 L11,6 L1,11 z" fill="#FFFFFF" stroke="#777"/></marker></defs>
<defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0,0 L10,5 L0,10 z" fill="#888"/></marker></defs>
<rect x="0" y="0" width="440" height="360" fill="#FFFFFF"/>
<rect x="140.0" y="12" width="110" height="31" rx="6" fill="#F7F7F7" stroke="#999999"/>
<text x="195.0" y="31.5" font-size="11.5" fill="#333" text-anchor="middle" font-weight="bold">Object</text>
<rect x="140.0" y="72" width="110" height="31" rx="6" fill="#FFF3E0" stroke="#E0A000"/>
<text x="195.0" y="91.5" font-size="11.5" fill="#7A5200" text-anchor="middle" font-weight="bold">Throwable</text>
<rect x="27.0" y="140" width="110" height="31" rx="6" fill="#FCE4EC" stroke="#D06A86"/>
<text x="82.0" y="159.5" font-size="11.5" fill="#9E3A57" text-anchor="middle" font-weight="bold">Error</text>
<rect x="253.0" y="140" width="110" height="31" rx="6" fill="#E9F2E9" stroke="#8FBF8F"/>
<text x="308.0" y="159.5" font-size="11.5" fill="#2E6B2E" text-anchor="middle" font-weight="bold">Exception</text>
<rect x="166.0" y="208" width="140" height="46" rx="6" fill="#EEF3FB" stroke="#4C7FE0"/>
<text x="236.0" y="227.5" font-size="11.5" fill="#26508F" text-anchor="middle" font-weight="bold">RuntimeException</text>
<text x="236.0" y="242.5" font-size="11" fill="#444" text-anchor="middle">(unchecked)</text>
<rect x="326.0" y="208" width="104" height="61" rx="6" fill="#E9F2E9" stroke="#8FBF8F"/>
<text x="378.0" y="227.5" font-size="11.5" fill="#2E6B2E" text-anchor="middle" font-weight="bold">IOException</text>
<text x="378.0" y="242.5" font-size="11.5" fill="#2E6B2E" text-anchor="middle" font-weight="bold">SQLException</text>
<text x="378.0" y="257.5" font-size="11.5" fill="#2E6B2E" text-anchor="middle" font-weight="bold">…</text>
<rect x="12.0" y="208" width="140" height="61" rx="6" fill="#FCE4EC" stroke="#D06A86"/>
<text x="82.0" y="227.5" font-size="11.5" fill="#9E3A57" text-anchor="middle" font-weight="bold">OutOfMemoryError</text>
<text x="82.0" y="242.5" font-size="11.5" fill="#9E3A57" text-anchor="middle" font-weight="bold">StackOverflowError</text>
<text x="82.0" y="257.5" font-size="11.5" fill="#9E3A57" text-anchor="middle" font-weight="bold">…</text>
<rect x="144.0" y="292" width="184" height="61" rx="6" fill="#EEF3FB" stroke="#4C7FE0"/>
<text x="236.0" y="311.5" font-size="11.5" fill="#26508F" text-anchor="middle" font-weight="bold">NullPointerException</text>
<text x="236.0" y="326.5" font-size="11.5" fill="#26508F" text-anchor="middle" font-weight="bold">IllegalArgumentException</text>
<text x="236.0" y="341.5" font-size="11.5" fill="#26508F" text-anchor="middle" font-weight="bold">…</text>
<polyline points="195,72 195,44" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
<polyline points="82,140 82,121.5 195,121.5 195,104" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
<polyline points="308,140 308,121.5 195,121.5 195,104" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
<polyline points="236,208 236,189.5 308,189.5 308,172" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
<polyline points="378,208 378,189.5 308,189.5 308,172" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
<polyline points="236,292 236,255" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
<polyline points="82,208 82,172" fill="none" stroke="#777" stroke-width="1.2" marker-end="url(#tri)"/>
</svg>
정리하면 이렇습니다.
Throwable— 던지고 처리할 수 있는 모든 것의 최상위 클래스.Error와Exception의 공통 부모.Error— JVM 수준의 심각한 문제. 애플리케이션이 복구하기 어렵습니다.Exception— 애플리케이션이 처리할 수 있는 문제. 다시 checked와 unchecked(RuntimeException)로 나뉩니다.
Throwable — 던지고 처리하는 모든 것의 최상위
Throwable은 "예외"라는 개념 자체를 대표하는 클래스입니다. throw 문에 올 수 있고 catch로 처리할 수 있는 건 오직 Throwable과 그 하위 타입뿐입니다 — 예컨대 throw "문자열"은 컴파일되지 않습니다.
예외를 처리할 때 사용하는 메서드도 전부 이 클래스에 정의돼 있습니다.
public class Throwable {
public String getMessage(); // 예외 메시지
public Throwable getCause(); // 원인 예외 (예외 체이닝)
public StackTraceElement[] getStackTrace(); // 스택 트레이스
public void printStackTrace();
// ...
}
즉 getMessage(), getCause(), 스택 트레이스 같은 기능은 Error든 Exception이든 모두 공통으로 가집니다. 둘의 차이는 기능이 아니라 "애플리케이션이 이걸 처리해야 하는가" 라는 용도에 있습니다.
Error — 처리하지 말아야 할 심각한 문제
Error는 JVM이나 시스템 수준에서 발생하는, 애플리케이션이 복구하기 어려운 심각한 상황을 나타냅니다. 대표적으로 OutOfMemoryError(힙 고갈), StackOverflowError(무한 재귀로 스택 초과), NoClassDefFoundError가 있습니다.
Error는 처리하라고 만든 게 아닙니다. 메모리가 바닥난 상황을 catch 한들 애플리케이션이 할 수 있는 일은 거의 없고, 억지로 무시하면 오히려 비정상 상태로 계속 동작하다가 더 큰 문제를 만듭니다. 그래서 Error는 보통 그대로 전파시켜 JVM이 스레드를 종료하도록 둡니다.
void recurse(int n) {
recurse(n + 1); // 끝없는 재귀 → 결국 StackOverflowError
}
이러한 StackOverflowError를 catch로 막는다고 근본 원인(잘못된 재귀)이 사라지지는 않습니다. Error가 보이면 처리할 게 아니라 코드를 고쳐야 한다는 신호로 읽는 편이 맞습니다.
Exception — 애플리케이션이 처리하는 문제
Exception은 애플리케이션 로직에서 충분히 발생할 수 있고, 처리할 가치가 있는 문제입니다. 파일이 없거나, 네트워크 연결이 종료되거나, 입력값이 잘못된 상황이 여기 속합니다. 그리고 Exception은 다시 두 가지로 나뉩니다.
1. checked 예외
Exception을 상속하되 RuntimeException은 아닌 것들(IOException, SQLException 등)입니다. 컴파일러가 반드시 try-catch로 처리하거나 throws로 전파하도록 강제합니다. 처리하지 않으면 컴파일 자체가 안 됩니다.
void read() throws IOException { // throws로 전파를 명시하거나
Files.readAllLines(path);
}
void read2() {
try {
Files.readAllLines(path); // 또는 여기서 직접 처리해야 컴파일된다
} catch (IOException e) {
// 복구 로직
}
}
2. unchecked 예외
RuntimeException과 그 하위 타입(NullPointerException, IllegalArgumentException, IndexOutOfBoundsException 등)입니다. 컴파일러가 처리를 강제하지 않습니다. 주로 프로그래밍 실수로 발생하므로, 일일이 처리하기보다 코드를 고쳐 애초에 발생하지 않게 하는 쪽이 맞습니다.
checked와 unchecked를 나누는 기준은 상속 관계 하나입니다 —
RuntimeException을 상속하면 unchecked,Exception만 상속하면 checked.Error도 unchecked로 분류됩니다(컴파일러가 처리를 강제하지 않는다는 의미에서).
catch (Throwable) vs catch (Exception)
이제 처음 질문을 다시 보면, 둘의 차이는 처리하는 범위입니다.
catch (Exception e)—Exception과 그 하위(checked + unchecked)만 처리합니다.Error는 그대로 전파됩니다.catch (Throwable t)—Exception뿐 아니라Error까지 전부 처리합니다.
try {
doSomething();
} catch (Throwable t) { // OutOfMemoryError·StackOverflowError까지 무시한다
log.error("무언가 실패", t);
}
이 코드는 OutOfMemoryError처럼 그냥 종료되는 편이 나은 상황까지 처리하려다 애플리케이션을 비정상 상태로 유지시킵니다. 그래서 일반적인 예외 처리에서는 catch (Throwable)을 사용하지 않고 catch (Exception)을 사용합니다.
Throwable을 처리해야 하는 경우는 많지 않습니다 — 스레드 풀, 프레임워크처럼 작업 하나가 어떤 이유로든 실패해도 전체가 멈추면 안 되는 최상위 경계에서, 로그를 남긴 뒤 다시 던지는(rethrow) 식으로 제한적으로 사용합니다. 그마저도 보통은 Exception으로 충분합니다.
한 줄 정리
| 구분 | 한마디로 | 대표 예 | 어떻게 대하나 |
|---|---|---|---|
Throwable | 던지고 처리할 수 있는 모든 것의 최상위 | (Error, Exception의 부모) | 직접 처리하지 않음 |
Error | JVM 수준의 심각한 문제(복구 불가) | OutOfMemoryErrorStackOverflowError | ❌ 처리하지 말고 그대로 전파 |
Exception(checked) | 충분히 예상되는 외부 상황 | IOExceptionSQLException | ✅ try-catch로 처리하거나 throws(컴파일러가 강제) |
RuntimeException(unchecked) | 주로 프로그래밍 실수 | NullPointerExceptionIllegalArgumentException | △ 처리하기보다 코드로 예방 (강제 없음) |
핵심만 요약하면 이렇습니다. Throwable은 최상위, Error는 처리하지 않고 전파할 심각한 문제, Exception은 처리할 문제 — 그리고 평소 예외 처리는 catch (Exception)이 기본입니다.
