【发布时间】:2010-09-13 04:05:10
【问题描述】:
当我的 C++ 方法遇到奇怪且无法恢复时,我想抛出异常。可以抛出std::string指针吗?
这是我期待做的事情:
void Foo::Bar() {
if(!QueryPerformanceTimer(&m_baz)) {
throw new std::string("it's the end of the world!");
}
}
void Foo::Caller() {
try {
this->Bar(); // should throw
}
catch(std::string *caught) { // not quite sure the syntax is OK here...
std::cout << "Got " << caught << std::endl;
}
}
【问题讨论】:
-
这是合法的,但不道德。
-
您有内存泄漏。谁在删除抛出的字符串指针?不要对异常使用指针。
-
我知道有点晚了,但不管怎样,这篇文章在这个问题上有很多点boost.org/community/error_handling.html