IQOD #11

Posts 1–7 of 7 · Page 1 of 1
IQOD #11
Only 1 response to IQOD #10? Are these getting too hard already?

Consider the following program:
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;
}
What output does it generate and why?
Unfortunately I am very much a novice, when it comes to programming.
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?
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
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.
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us