[英]Unable to catch a std::runtime_error
也許我今天沒有喝咖啡。 以下程序應捕獲std :: runtime_error並顯示“我捕獲了runtime_error”,對嗎?
它不是。 該程序未捕獲std :: runtime_error,而是顯示“為什么我無法捕獲runtime_error”?
我在這里做錯了什么? 為什么我沒有趕上std :: runtime_error?
這是Clang(請參見下面的代碼,環境信息)。
#include <iostream>
#include <exception>
int main(int argc, const char * argv[])
{
try
{
throw new std::runtime_error( "a runtime_error was thrown" );
}
catch ( const std::runtime_error& e )
{
std::cout << "i caught the runtime_error" << std::endl;
}
catch ( ... )
{
std::cout << "why was i unable to catch the runtime_error?" << std::endl;
}
return 0;
}
OS X 10.9.5上的Xcode 5.1.1
comp:~ usrn$ clang --version
Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
comp:~ usern$
您正在拋出new std::runtime_error( "a runtime_error was thrown" );
,
因此,您將拋出std::runtime_error*
。
您可能想要throw std::runtime_error("...")
,即按值拋出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.