Exception and rethrow

T

Tony Johansson

Hi!
I'm reading the e-learning from Microsoft and there is one thing that I hope
somebody can answer.

In the Best practice section below it says that if I rethrow an existing
exception the stack trace is maintained.
But if I read the description section below I get the impression that the
origination stack trace is removed.

....
So my question is if I rethrow an existing exception in the catch part like
this
catch(Exception e)
{
throw e;
}
will the stack trace be maintained from where the origination place where
the exception occured ?


Best practice
Use the throw keyword without specifying an exception to rethrow an
exception while maintaining the stack trace.

Description
The runtime generates and maintains a stack trace from the point at which an
exception is thrown. If you rethrow the exception, the stack trace is
cleared and the originating point of the exception is lost. When you need to
rethrow an exception without wrapping it in another exception, the correct
syntax is to use the throw keyword without specifying an exception. The
original exception will propagate up with its stack trace intact.

//Tony
 
A

Adam Clauss

Tony said:
Hi!
I'm reading the e-learning from Microsoft and there is one thing that I hope
somebody can answer.

In the Best practice section below it says that if I rethrow an existing
exception the stack trace is maintained.
But if I read the description section below I get the impression that the
origination stack trace is removed.

...
So my question is if I rethrow an existing exception in the catch part like
this
catch(Exception e)
{
throw e;
}
will the stack trace be maintained from where the origination place where
the exception occured ?
No, as the documentation said:
"If you rethrow the exception, the stack trace is cleared and the
originating point of the exception is lost."

If you are trying to keep the existing stack trace, do what the next two
sentences say:
"When you need to rethrow an exception without wrapping it in another
exception, the correct syntax is to use the throw keyword without
specifying an exception. The original exception will propagate up with
its stack trace intact."

That is:

catch (Exception e)
{
throw; // Note: "e" is not included on this line
}
 
P

Peter Duniho

Tony said:
Hi!
I'm reading the e-learning from Microsoft and there is one thing that I hope
somebody can answer.

In the Best practice section below it says that if I rethrow an existing
exception the stack trace is maintained.

Yes, it says that, but with a subtly different meaning from other uses
of the phrase "rethrow an exception" found elsewhere in the document.
IMHO, this is a documentation problem.
But if I read the description section below I get the impression that the
origination stack trace is removed.

....
So my question is if I rethrow an existing exception in the catch part like
this
catch(Exception e)
{
throw e;
}
will the stack trace be maintained from where the origination place where
the exception occured ?

This is actually yet another example of "things that are trivial to
observe for yourself".

That said…
Best practice
Use the throw keyword without specifying an exception to rethrow an
exception while maintaining the stack trace.

This wording is confusing, especially in juxtaposition with the following:
Description
The runtime generates and maintains a stack trace from the point at which an
exception is thrown. If you rethrow the exception, the stack trace is
cleared and the originating point of the exception is lost.

There are two different meanings to "rethrow the exception" here. One
is the syntax "throw;" and the other is the syntax "throw e;". The
former is an example of using "the throw keyword without specifying an
exception", and will preserve the stack trace in the original exception.

IMHO, they should not have written "…to rethrow an exception while
maintaining the stack trace" in the "Best practice" section. A
different wording could have made things much clearer.

The bottom line: if you specify a target exception with the "throw"
keyword, you get a new stack trace. If you simply use the "throw"
keyword alone, the original stack trace is preserved. Both uses could
be described as "rethrowing the exception", but IMHO using that same
phrase to describe both makes the whole documentation almost pointless,
because it doesn't effectively distinguish between the two scenarios.

Pete
 

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

Top