Hi All,

Could you please clear my doubt why this second catch block is called when exception is thrown only once.

#include <iostream>
using namespace std;

class E {
public:
    const char* error;
    E(const char* arg): error(arg) {}
};

class B {
public:
    B() {};
    ~B(){cout<<"~B() called"<<endl;}
};

class D: public B {
public:
    D();
    ~D() { cout<<"~D() called"<<endl; }
};

D::D() try :B(){
    throw E("Exception in D");
} catch(E&e)
{
    cout<<"D()"<<e.error<<endl;
};

int main()
{
    try {
        D val;
    }catch(...) { cout<<"Last"<<endl;}
}

Paragraph 15.3.15 of the standard: "The currently handled exception is rethrown if control reaches the end of a handler of the function-try-block of a constructor or destructor."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.