Checked vs. Unchecked Exceptions in Service Layer

前端 未结 5 835
自闭症患者
自闭症患者 2020-12-13 10:59

I work on a project with a legacy service layer that returns null in many places if a requested record does not exist, or cannot be accessed due to the caller not being auth

5条回答
  •  被撕碎了的回忆
    2020-12-13 11:54

    I see the checked/unchecked exception in following perspectives, (taking clue from the standard JDK)

    1. Use Checked exception if you code/library depends on some resource which is external to the JVM (e.g .file/socket/remote api etc i.e JVM has no control over it). Your code MUST handle all the possible error conditions that source may result. This is to make sure that your program is robust and fails gracefully in case something is wrong with that resource.

    2. Unchecked exception are the serious error conditions OR the real BUGS in your code which generally should not be handled AFTER it has occurred. You could correct your code so that this error does not show in the first place. (Eq. NLP, Class cast, Divide by Zero etc)

提交回复
热议问题