Should I inherit from std::exception?

前端 未结 14 1506
有刺的猬
有刺的猬 2020-11-30 18:56

I\'ve seen at least one reliable source (a C++ class I took) recommend that application-specific exception classes in C++ should inherit from std::exception. I\

14条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 19:36

    The problem with std::exception is that there is no constructor (in the standard compliant versions) that accepts a message.

    As a result I prefer to derive from std::runtime_error. This is derived from std::exception but its constructors allow you to pass a C-String or a std::string to the constructor that will be returned (as a char const*) when what() is called.

提交回复
热议问题