带有消息的 C++ 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17438863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Exceptions with message
提问by Alex Netkachov
I'm not sure that my custom exception approach is correct. What I want to do is to throw exceptions with custom messages but it seems that I created a memory leak...
我不确定我的自定义异常方法是否正确。我想要做的是使用自定义消息抛出异常,但似乎我创建了内存泄漏...
class LoadException: public std::exception {
private:
const char* message;
public:
LoadException(const std::string message);
virtual const char* what() const throw();
};
LoadException::LoadException(const std::string message) {
char* characters = new char[message.size() + 1];
std::copy(message.begin(), message.end(), characters);
characters[message.size()] = 'void array_type_guard(Local<Value> obj, const std::string path) {
if (!obj->IsArray()) {
throw LoadException(path + " is not an array");
}
}
try {
objects = load_objects();
} catch (std::exception& e) {
ThrowException(Exception::TypeError(String::New(e.what())));
return scope.Close(Undefined());
}
';
this->message = characters;
}
I use it as follows:
我使用它如下:
class LoadException: public std::exception {
private:
const char* msg;
public:
LoadException(const std::string message);
virtual const char* what() const throw();
};
LoadException::LoadException(const std::string message) {
msg = message.c_str();
}
const char* LoadException::what() const throw() {
return msg;
}
I afraid that the array created in constructor is never deleted. But I'm not sure how to delete it - should I add destructor or maybe use completely different approach?
我担心在构造函数中创建的数组永远不会被删除。但我不确定如何删除它 - 我应该添加析构函数还是使用完全不同的方法?
Update:
更新:
I've actually tried to use the string class as follows:
我实际上尝试使用字符串类如下:
class LoadException: public std::exception {
private:
std::string message_;
public:
explicit LoadException(const std::string& message);
virtual const char* what() const throw() {
return message_.c_str();
}
};
LoadException::LoadException(const std::string& message) : message_(message) {
}
But cannot get the error message then - some random output is displayed when I print the "what()".
但是无法获得错误消息 - 当我打印“what()”时会显示一些随机输出。
回答by MonoThreaded
How aboutthrow std::runtime_error("My very own message");
怎么样throw std::runtime_error("My very own message");
回答by NG.
You can take advantage of std:string
你可以利用 std:string
Printer::Printer(boost::asio::io_service& io, unsigned int interval) {
if (interval < 1) {
throw std::runtime_error("Interval can't be less than one second");
}
}
Then the C++ scoping will take care of cleaning things up for you
然后 C++ 范围将负责为您清理一切
回答by kometen
In the constructor I have
在构造函数中我有
try {
Printer p{io, 0};
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
And when creating the object
在创建对象时
##代码##the program will exit with the message thrown.
程序将退出并抛出消息。