exception

G

Grahamo

My question pertains to exceptions and their destruction

Say I do this in my (pseudo) code;

class an_exception
{
// details/methods for exception etc.
~an_exception() { .... }; };

try
{
foo();
}
catch (const an_exception& ex)
{
// handle the exception

}


void foo() throw (an_exception)
{
if (something)
an_exception ex;
throw (ex);
}


At what point is the destructor for the an_exception object called? Is
a copy taken at the point of "throw" and the "ex" instance destroyed
before the throw statement executes?

the reason I ask is to clarify another piece of code I saw whereby the
exception is newed and then thrown. The caller catches a pointer to the
exception. something like;

throw (new an_exception());

and the calling code obviously changes to handle the throw.

Anyhows I'm more interested in the pseudo code for the moment just to
get a handle on what happens when the exception is thrown by value.
 
G

Grahamo

that should be ...

class an_exception
{
// details/methods for exception etc.
~an_exception() { .... }; };
} /////////// FORGOT THIS IN FIRST POST...
 
V

Victor Bazarov

that should be ...

class an_exception
{
// details/methods for exception etc.
~an_exception() { .... }; };
} /////////// FORGOT THIS IN FIRST POST...

Yes, but you have two '};' after the '....' in your d-tor so the other
closing brace doesn't seem necessary. OTOH you have your 'try-catch'
outside of any function, which is rather weird... Try posting real
code next time.

V
 
R

Rolf Magnus

At what point is the destructor for the an_exception object called?

Whenever one of them gets destroyed.
Is a copy taken at the point of "throw" and the "ex" instance destroyed
before the throw statement executes?

Likely. There might also happen additional copies during the stack
unwinding.
the reason I ask is to clarify another piece of code I saw whereby the
exception is newed and then thrown. The caller catches a pointer to the
exception.

Never do that. There is no good reason to throw by pointer.
something like;

throw (new an_exception());

and the calling code obviously changes to handle the throw.

Anyhows I'm more interested in the pseudo code for the moment just to
get a handle on what happens when the exception is thrown by value.

It might be copied once or several times. It's platform dependant.
 
G

Grahamo

thanks for the replies....

"Say I do this in my (pseudo) code; "
Try posting real code next time.

try reading the post next time.

Never do that. There is no good reason to throw by pointer.

I thought that myself. It's not my code in any case but I'll point it
out to the owner.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top