What type of exception should I throw?

后端 未结 5 1185
难免孤独
难免孤独 2020-12-08 03:40

After going through some links on exception handling (1, 2, and 3), I know that C++ programs can throw pretty much anything as exceptions (int, char*

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 04:03

    You would derive your own class from std::exception, so that there is some way of uniformly handling exceptions.

    If this seems like overkill, you can throw std::logic_error or one of the other standard exception types intended for applications to use.

    You could also use these as base classes for your own more specific exceptions: this saves a little work because they take care of implementing the what method for you.

    Note that deep exception hierarchies can be unusable, because you're basically making a guess in advance about how to categorize the errors, and your clients may disagree.

提交回复
热议问题