不用尝试在构造函数中捕获块

时间:2016-07-16 19:43:54

标签: c++ exception constructor

我有以下代码:

#include <iostream>
using namespace std;

int foo()
{
    throw 1;
}

struct A {
    int a;
    public:
    A() try : a(foo())
    {
        cout << "Constructor A\n";
    } catch(...)
    {
        cout << "Catched in A\n";
    }
};

struct B : A {
    B()
    {
        cout << "Constructor B\n";
        ::foo();
    } catch(...)
    {
        cout << "Catched in B\n";
    }
    void foo()
    {

    }
    catch(...)
    {
        cout << "Catched in foo\n";
    }
};

int main () try
{
    B b;
    return 0;
} catch (...)
{
    cout << "Catched in main\n";
}

输出:

  

抓住A

     

抓住主要

为什么B的阻挡块没有捕获任何东西?如果没有尝试它怎么能存在?

Clang只检测第26行的错误: error: expected member name or ';' after declaration specifiers并认为B::foo的阻止是正常的。我很困惑:)

Clang ++ v 3.4

gcc v 4.8

0 个答案:

没有答案