Java best practices when throwing exceptions: throwing core Java exceptions

后端 未结 5 1144
南方客
南方客 2021-02-02 09:07

Instead of throwing new Exception(\"Some message\", maybeSomeCause), which means that all callers of my method will need to catch Exception (which can include Runti

5条回答
  •  梦如初夏
    2021-02-02 09:15

    If users of your code might need to do different things on two different exceptions, then those should be distinct exception types. That said, the JDK exceptions cover most of the "programmer error" exceptions -- if an IllegalArgumentException is getting thrown, for example, that indicates a programming mistake, not something that should be handled at runtime.

提交回复
热议问题