Smart Recent Blog Post List

Mar 10, 2010

C++ Brain Food : A must solve c++ question.

I really like this c++ question. It really helps us to understand exception handling mechanism of c++.

Lets check you will get it right or not:-

Don't just execute code with compiler to get the result, try out by doing dry run.

What output does this program generate as shown? Why?

#include

using namespace std;

class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw "A::exception";
}
};

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

int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;

A objectA;
B objectB;

cout << "Exiting try...catch block" << endl;
} catch (const char* ex) {
cout << ex << endl;
}

return 0;
}






0 comments:

Post a Comment

Smart Recent Blog Post List