새로운 예외는 동작 메서드를 오버라이드해야 합니다. 코드, 설명, 재실행 등을 갖는 것은 동작
이 아닙니다. 모든
Person
인스턴스에서 서로 다른 이름을 반환하는 것은 원하지 않을 것입니
다. 그렇다면 예외에서는 왜 이렇게 하는 걸까요? 특정한 예외를 얼마나 자주 잡아내나요? 코
드를 확인해보세요. 반드시 클래스여야 할 필요가 있을까요? 이미 클래스에 결합되어 있습니
다. 그렇다면 설명에 대신 결합하세요. 예외 인스턴스는 싱글턴이 되어서는 안 됩니다.
관련 레시피
●
3
.
1
절 ‘빈약한 객체를 풍성한 객체로 변환하기’
●
19
.
9
절 ‘비어 있는 클래스 마이그레이션하기’
22.3
예상 가능한 상황을 위한 예외 재작성하기
문제
예상되고 유효한 비즈니스 사례에 대해 예외를 사용합니다.
해결
제어 흐름에 예외를 사용하지 마세요.
설명
예외는
goto
, 플래그와 유사하며(
18
.
3
절 ‘
goto
를 구조화된 코드로 대체하기’ 참조), 일
반적인 경우에 사용하면 가독성이 떨어지며 놀람 최소화 원칙을 위반합니다(
5
.
6
절 ‘변경 가능
한 상수 고정하기’ 참조). 예외는 예상치 못한 상황에만 사용해야 하며 계약 위반만을 처리해야
합니다(
13
.
2
절 ‘전제 조건 적용하기’ 참조).
다음은 무한 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.