c ++琐碎的try-catch导致中止

giu*_*pen 4 c++

下面的简单代码

// g++ centro.cc -o centro

#include <iostream>
using namespace std;

int  main(int argc, char *argv[])
{
    try
    {
        cout << "Going to throw" << endl;
        throw;
    }
    catch(...)
    {
        cout << "An exception occurred" << endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

产生中止:

Going to throw
terminate called without an active exception
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

我不明白什么是错的,有人能指出我正确的方向吗?

Kos*_*Kos 8

尝试一些东西.你没有抛出任何异常.

throw;本身通常用于在catch块内重新抛出相同的异常.

将结果与throw "something";或可能是一个实例进行比较std::exception.


eca*_*mur 7

你的路线

throw;
Run Code Online (Sandbox Code Playgroud)

是在catch块中重新抛出异常的语法.

你应该写:

throw std::exception();
Run Code Online (Sandbox Code Playgroud)