我有一个异常类如下:
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为空。 如果有人可以提供帮助,我很乐意欣赏它。
I have an exception class as follows:
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; };And I throw this exception like this:
std::string message = "Message"; throw ::FileNotFoundException(message.c_str());But when I try to handle it using:
try { // the code that throws } catch(::FileNotFoundException& ex) { std::string message = ex.what(); }The string is empty. If anyone can help, I would gladly appreciate it.
最满意答案
您不能只存储指向消息的指针。 尝试将其存储在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有一个构造函数,可以正确地存储消息。 可以在差异中找到完整的解释:std :: runtime_error vs std :: exception()
我认为这种方法比定义what()和使用std::string自己存储消息更好。 也就是说,如果您对异常类没有特殊需求。
您还应该检查C ++异常层次结构 。
You can't just store a pointer to the message. Try either storing it in std::string, or, better, pass it to the parent constructor. Perhaps it's better to inherit from std::runtime_error in that case.
Here's a complete example:
#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; } }Compiling and running:
$ g++ -W -Wall --std=gnu++11 a.cpp -oa $ ./a Exception: 'oops, something happened'In short (and without details): The class std::exception doesn't have any constructors. It's just a parent class used by all other exceptions. On the other hand, std::runtime_error has a constructor that stores the message for you properly. A complete explanation can be found in Difference: std::runtime_error vs std::exception()
I think this approach is better than to define what() and using std::string to store the message yourself. That is, if you don't have special needs for the exception class.
You should also check out the C++ exception hierarchy.
更多推荐
发布评论