woowacourse / tecoble-comments

0 stars 0 forks source link

tecoble/post/2020-08-17-custom-exception/ #19

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

custom exception을 언제 써야 할까?

우아한테크코스의 두 크루인 오렌지와 우가 싸우고 있다. 왜 싸우고 있는지 알아보러 가볼까? 오렌지 : 아니 굳이 사용자 정의 예외 안 써도 됩니다!! 우 : 아닙니다!! 써야 합니다!!! 사용자 정의 예외(Custom Exception…

https://woowacourse.github.io/tecoble/post/2020-08-17-custom-exception/

tmdgusya commented 3 years ago

좋은 글이네요. 덕분에 예외에 대해서 조금 더 생각해보게 되는 계기였습니다. Effective Java 에 나오는 내용처럼 추상화 수준에 맞는 예외를 던질때도 사용하기 좋다고 생각됩니다. 예를 들면 list 에서 여러 원소들을 순회할때 해당 원소가 없으면 NoSuchElementException 을 던지는데, 이를 IndexOutOfBoundsException 로 감싸서 발생시키더라고요.

위와 같이 이 Exception 이 어떻게 Tracking 될 수 있는가? 어떤 의미로서 전파되는게 맞는가? 도 중요한 논제같네요.

 catch (NoSuchElementException e) {
        throw new IndexOutOfBoundsException("인덱스: " + index);
    }