Java, Class-specific Exceptions vs. Standard Exceptions

前端 未结 6 1811
北恋
北恋 2021-02-07 12:42

I\'ve been updating an existing library to throw exceptions to help improve debugging by people using the library.

At first, I thought I\'d define exceptions specific to

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 13:19

    Extending the Exceptions without adding any value like this is a complete waste of time and incurs an ongoing maintenance cost that is best avoided.

    Use the standard exceptions.

    This is not to say that you should never use custom exceptions, just not in the use cases you present.

    Also, when creating custom exceptions, they should be related to the conditions which cause them, not the class from which they might be thrown. It is OK to relate them to a business/functional area, as error conditions which cause exceptions are likely related in this way, and it will provide useful filtering techniques.

提交回复
热议问题