Skip to content
Minhyung Park

effective kotlin - Prefer standard errors to custom ones

kotlin, programming1 min read

TL;DR

  • Stardard library의 error 로는 설명이 부족할때는 custom error를 사용해라
  • 하지만, 가능한 한 Standard library의 error를 사용해라

When we need to use custom error

  • 특정 상황에 발생시켜야하는 error가 Std lib에서 제공하는 error들 중에서 적합한 것이 없을 경우에만 사용
    • example) JSON format이 맞지 않을 경우 JsonParsingException 과 같은 error를 정의하여 사용

Why do we use standard errors?

  • 대부분의 개발자들이 standard error들을 잘 알고 있다.
  • 따라서, 해당 API를 개발한 개발자 외의 다른 개발자들이 사용할 때 더 쉽게 더 빠르게 이해할 수 있다.

Most common exceptions supported by standard library

  • IllegalArgumentException
  • IllegalStateException
  • IndexOutOfBoundsException
  • ConcurrentModificationException
  • UnsupportedOperationException
  • NoSuchElementException