Best practices for defining your own exception classes?

前端 未结 5 799
走了就别回头了
走了就别回头了 2020-12-28 12:57

I have some special exception cases that I want to throw and catch, so I want to define my own exception classes.

What are the best practices for that? Should I inh

5条回答
  •  孤独总比滥情好
    2020-12-28 13:58

    In my opinion it doesn't matter if you inherit from std::exception or not. For me the most important thing about defining exceptions are:

    1. Having the exception class names be useful and clear.
    2. Clearly documenting (writing comments) when exception will get thrown by a function or class method. This is the single biggest failure point in exception handling in my opinion.

提交回复
热议问题