[英]Terminate called without an active exception
This is my code : 这是我的代码:
#include<iostream>
#include<boost/shared_ptr.hpp>
using namespace std;
void func()
{
cout<<" func # "<<endl;
throw;
}
int main()
{
try
{
int x = -1;
cout<<" point 1 "<<endl;
func();
cout<<" point 2 "<<endl;
}
catch (exception& e)
{
cout<<" exception caught "<<endl;
//throw;
}
cout<<" point 3 "<<endl;
return 0;
}
Now, it's giving this result 现在,它给出了这个结果
point 1
func #
terminate called after throwing an instance of in
Abort
But I was expecting this: 但我期望这样:
point 1
func #
exception caught
What am I missing? 我想念什么?
Why is terminate being called like this? 为什么终止如此称呼?
And also, what if I also throw from catch block? 而且,如果我也从挡球区扔了怎么办?
This is because func
has an empty throw
statement. 这是因为
func
有一个空的throw
语句。 If that statement is executed without an active exception being handled, terminate is supposed to be called. 如果该语句没有被处理活动异常执行,终止应该被调用。
If no exception is presently being handled, evaluating a throw-expression with no operand calls std::terminate().
如果当前未处理任何异常, 则对不带操作数的throw-expression求值将调用std ::: terminate()。
You need to throw something in order to catch. 您需要扔东西才能赶上。 An empty throw statement only has something to throw while an exception is being handled.
空的throw语句仅在处理异常时才会抛出异常。
You probably meant to write throw std::exception{};
您可能打算编写
throw std::exception{};
And also, what if I also throw from catch block?
而且,如果我也从挡球区扔了怎么办?
Assuming you apply the fix, the empty throw
in the exception handler (the catch
block) will re-throw the exception you caught from inside func
. 假设您应用了此修复程序,则异常处理程序(
catch
块)中的空throw
将重新引发您从func
内部捕获的异常。 And now std::terminate
will be called because an uncaught exception is about to leave the main
function. 现在将调用
std::terminate
因为未捕获的异常即将离开main
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.