小编And*_*ndy的帖子

c++ std::exception "what()" 消息将始终只在自定义异常中打印 "std::exception" - 为什么我不能让它打印内容?

如果我这样做:

//... in .h header:

class MyCustomException : public std::exception{
  private:
    string message;

  public:
    MyCustomException (string msg); //in .cpp implements "message = msg"
    ~MyCustomException() throw(){ }
    string what(); //implemented to return message.

}
Run Code Online (Sandbox Code Playgroud)
//... other code ... in cpp:

if (<blah blah blah>){
  throw MyCustomException("I want to see THIS message when I fail ... but I don't...");
}

//... more code, in cpp:

//try{<blah...>}
catch(const:: std::exception& e){
   cout << e.what() << endl << flush; //just prints "std::exception" ... same if …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance exception object-slicing c++98

1
推荐指数
1
解决办法
355
查看次数

标签 统计

c++ ×1

c++98 ×1

exception ×1

inheritance ×1

object-slicing ×1