코드를 까보다가 이런 주석을 발견했다.

저 method에 진입했을 때 null로 return 시켜서 method를 호출하는 부분에서 exception을 시키는 코드인데, 주석에 저 상태에서 Exception을 발생시키라고 되어있었다.
null을 보내는 것 보다 Exception을 발생시키는 것이 좋기야 하겠지만, 저 코드를 실행시켰을 때는 둘의 차이가 없는 것 같아 둘의 차이가 무엇일지 궁금해 찾아보았더니
https://chrisshennan.com/blog/return-null-or-throw-exception-best-practice
Return null or Throw Exception - Best Practice?
Returning `null` is the programmatic equivalent of "It's broken". Use exceptions to give context and allow you to tailor your error handling.
chrisshennan.com
https://www.code4it.dev/cleancodetips/exceptions-instead-of-null
Clean Code Tip: throw exceptions instead of returning null when there is no fallback
In case of unmanageable error, should you return null or throw exceptions?
www.code4it.dev
이런 글들이 나왔다...!! 이게 다들 궁금함을 가지는 내용이구나💡
저 코드에서 디버깅을 돌려 한줄씩 내려가보니 exception을 발생시키는 위치가 다르다는 것을 알게되었다. 결국은 발생시키는 시점을 어디서 하느냐를 조정할 수 있고 더 클린코드로 쓸 수 있는 것 같다.!!
'TIL' 카테고리의 다른 글
[TIL] 서버에서 배포 시에 실패 Exit 뜰 때와 jdk버전차이 확인하기 (0) | 2023.07.13 |
---|---|
[TIL] 생성자 관련 어노테이션 @NoArgsConstructor @RequiredArgsConstructor @AllArgsConstructor (0) | 2023.07.04 |
[TIL] Spring boot @Deprecated 어노테이션 (0) | 2023.06.27 |
[TIL] git flow - hotfix branch 를 main에 merge한 이후에? (0) | 2023.06.22 |
[TIL] 버퍼를 읽는 것이란 무엇일까...?... Wrapper class를 써보았다. (0) | 2023.06.20 |