-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41b7fc5
commit 9785761
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# 표준 예외를 사용하라 | ||
### 표준 예외의 장점 | ||
|
||
- 많은 사람들과 약속된 규약을 따르는 것 - 모르는 사람이 코드를 읽기 편해짐 | ||
- 익숙한 예외이기 때문에 읽기 쉬움 | ||
- 예외 클래스를 별도로 만들 필요가 없어 메모리 절약 | ||
|
||
### 가장 많이 사용되는 표준 예외 | ||
|
||
#### NullPointerException (NPE) | ||
|
||
- Null을 허용하지 않는 메서드에 null을 건넸을 때 발생하는 예외 | ||
|
||
#### IndexOutOfBoundsException | ||
|
||
- 인덱스의 범위를 넘어섰을 때 발생하는 예외 | ||
|
||
#### IllegalArgumentException | ||
|
||
- 호출자가 부적절한 값을 인수로 넘길 때 던지는 예외 | ||
- Null이 들어오면 IllegalArgumentException보다는 **NullPointerException**을 던진다. | ||
- 시퀀스의 범위를 넘는 값이 들어오면 IllegalArgumentException보다는 **IndexOutOfBoundsException**을 던진다. | ||
|
||
#### IllegalStateException | ||
|
||
- 대상 객체의 상태가 호출된 메서드 수행에 적합하지 않을 때 던지는 예외(초기화되지 않은 객체를 사용하려 할 때) | ||
|
||
#### ConcurrentModificationException | ||
|
||
- 단일 스레드에서 사용하려고 설계한 객체를 여러 스레드가 동시에 수정하려 할 때 던지는 예외 | ||
|
||
#### UnsupportedOperationException | ||
|
||
- 클라이언트가 호출한 메서드를 지원하지 않을 때 발생하는 예외 | ||
|
||
## 다른 상황에 재사용할 수 있는 예외 | ||
|
||
#### ArithmeticException | ||
|
||
- 복소수나 유리수를 다루는 객체를 다룰 때 사용할 수 있는 예외 (10 / 0과 같은 연산을 수행하려는 경우 발생) | ||
|
||
#### NumberFormatException | ||
|
||
- 복소수나 유리수를 다루는 객체를 다룰 때 사용할 수 있는 예외 (Integer 파라미터 자리에 String이 오는 경우 발생) | ||
|
||
### 예외 재사용 시 주의 사항 | ||
|
||
- **Exception, RuntimeException, Throwable, Error**는 직접 재사용하지 말자. 여러 성격의 예외들을 포괄하고 있기 때문에 안정적으로 테스트할 수 없고 정확한 예외를 넘기기 어려움 | ||
|
||
- 예외는 직렬화할 수 있어 많은 리소스가 필요하기 때문에 꼭 확장이 필요한지 확인 |