简体   繁体   English

在没有活动异常的情况下终止调用

[英]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. 如果该语句没有被处理活动异常执行,终止应该被调用。

[expr.throw]/4 [投掷次数] / 4

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM