Unfortunately I am very much a novice, when it comes to programming.
Only 1 response to IQOD #10? Are these getting too hard already?
Consider the following program:
What output does it generate and why?Code:#include <iostream> 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(char *ex) { cout << ex << endl; } return 0; }
Last edited by B1ackAnge1; 09-25-2009 at 11:03 AM.
Unfortunately I am very much a novice, when it comes to programming.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
i am only at chapter four yet
Entering try...catch block
A::A()
B::B()
B::exception
B::~B()
A::~A()
shut down/crash since it only catches 1 exception.
amiright?
Ah we-a blaze the fyah, make it bun dem!
HD: <!-- Incorrect.. but you're half way there with figuring what's happening -->
hmm
Entering try...catch block
A::A()
B::B()
B::exception
A::~A()
A::exception
B::~B()
Exiting try...catch block
if that is incorrect i give up on this one xD I hate exceptions
Ah we-a blaze the fyah, make it bun dem!
Noticed I hadn't replied to this one yet...
What happens is:
We Enter the Try Block
"Entering try...catch block"
A Get's Constructed, output:
A::A()
B Get's Construct, output:
B::B()
B Now throws an exception which gets caught by the catch handler. However this means that at this point A is going out of scope
A::~A()
However now in the destructor of A it throws another exception. Since we're not in a try-catch anymore the program at this point crashes on the exception.