What factors should be taken into consideration when writing a custom exception class?

后端 未结 7 928
甜味超标
甜味超标 2020-12-29 15:38

When are custom Exception classes most-valuable?
Are there cases when they should or should not be used? What are the benefits?

Related

相关标签:
7条回答
  • 2020-12-29 16:33

    As Wheelie said, look first for an appropriate framework exception, and only use exceptions (especially custom exceptions) where you really plan to catch them.

    I use a custom exception class that contains a UserMessage property, so that I can propagate the problem in plain language to the user when possible.

    If you're using .NET, check out Designing Custom Exceptions. Interestingly, the documentation has changed its recommendation on the use of ApplicationException:

    If you are designing an application that needs to create its own exceptions, you are advised to derive custom exceptions from the Exception class. It was originally thought that custom exceptions should derive from the ApplicationException class; however in practice this has not been found to add significant value. For more information, see Best Practices for Handling Exceptions.

    0 讨论(0)
提交回复
热议问题