异常消息为空

时间:2017-04-11 09:31:30

标签: c++ exception stdstring

我有一个例外类如下:

class FileNotFoundException : public std::exception
{
public:
    FileNotFoundException(const char* message) : errorMessage(message){ }
    const char* what() const throw() override
    {
        return this->errorMessage;
    }
private:
    const char* errorMessage;
};

throw这样的例外:

std::string message = "Message";
throw ::FileNotFoundException(message.c_str());

但是当我尝试使用以下方法处理它时:

try
{
    // the code that throws 
}
catch(::FileNotFoundException& ex)
{
    std::string message = ex.what(); 
}

string为空。 如果有人可以提供帮助,我将很乐意欣赏它。

2 个答案:

答案 0 :(得分:2)

您不能只存储指向消息的指针。尝试将其存储在std::string中,或者更好地将其传递给父构造函数。在这种情况下,从std::runtime_error继承可能会更好。

这是一个完整的例子:

#include <iostream>
#include <string>
#include <stdexcept>

class FileNotFoundException : public std::runtime_error
{
public:
  FileNotFoundException(const char* message) : std::runtime_error(message)
  {
  }
};

int main()
{
  try {
    throw ::FileNotFoundException("oops, something happened");
  }
  catch(const ::FileNotFoundException& ex) {
    std::cout << "Exception: '" << ex.what() << "'" << std::endl;
  }
}

编译并运行:

$ g++ -W -Wall --std=gnu++11 a.cpp -oa
$ ./a
Exception: 'oops, something happened'

简而言之(没有细节):班级std::exception没有任何构造函数。它只是所有其他异常使用的父类。另一方面,std::runtime_error有一个构造函数,可以正确地存储消息。可以在Difference: std::runtime_error vs std::exception()

中找到完整的说明

我认为这种方法比定义what()并使用std::string自己存储消息更好。也就是说,如果您对异常类没有特殊需求。

您还应该查看C++ exception hierarchy

答案 1 :(得分:2)

你的问题在这里:

throw ::FileNotFoundException(message.c_str());

您正在异常中存储指向message拥有的内存的指针。当message超出范围(在投掷期间发生)时,数据将不再有效。这意味着this->errorMessage返回一些未定义的内存。要修复它,您可以将一些非常常量的字符串传递给您的异常,或者您需要拥有该字符串的异常,例如将errorMessage设为std::string