【发布时间】:2014-04-23 20:15:14
【问题描述】:
我得到这个以未捕获的异常终止,即使我认为我已经捕获了异常。这是一些示例代码
#include <iostream>
#include <stdexcept>
void throwing(int x)
{
if(x) throw std::runtime_error("x non-null");
}
int main()
{
try {
throwing(1);
} catch(std::runtime_error const&ex) {
std::clog << "error: \"" << ex.what() << '\"' << std::endl;
std::terminate();
}
return 0;
}
产生(在报告错误:“x non-null”之后)所述消息(使用带有std=c++11的clang++)。那么,什么时候准确地 捕获了异常,因此在terminate() 的意义上认为未捕获 没有(再次)报告它?或者,等效地(或不是?):我怎样才能捕获一个异常,报告它的what() 和terminate() 而不会得到这个简介?
(我可以避免举报what(),只举报terminate(),但我想以我自己的方式举报what()。)
【问题讨论】:
-
你应该阅读一下
terminate。 cplusplus.com/reference/cstdlib/abort -
我认为这是因为您在
catch块中仍然有一个活动异常。解决问题的一种方法是直接调用std::abort()而不是std::terminate()。 -
@Praetorian 那么uncaught是指active吗?那么为什么它会报告 uncaught 异常(而不是 active 异常)?
-
好吧,根据定义,未捕获的异常是活动的,直到您的程序终止。我在gcc or clang output 中都没有看到任何提及未捕获的异常
-
@Praetorian 1 问题不是未捕获的异常是否处于活动状态,而是活动的异常是否未捕获 . 2 我得到 terminating with uncaught exception of std::runtime_error with clang (v3.3, OS/X 10.9) 和 terminate 在抛出一个实例后调用'std::runtime_error' 与 gcc (4v.9)
标签: c++ exception try-catch terminate