By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

re-throwing an exception

P: n/a
Hi,

I would like to re-throw an exception but keep the stack trace from
when the exception was originally generated. Is there any way of doing
this (using .Net 1.1)?

I am not throwing it from the same place it was caught (in fact it is
caught in a worker thread which squirrels it away and then the main
thread digs it out later and throws it)

I realise that I could wrap the exeption, but then any calling function
wishing to catch it would have to take special action (to catch the
wrapper rather than the original)

Andy

Mar 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ajf...@blueyonder.co.uk wrote:
I would like to re-throw an exception but keep the stack trace from
when the exception was originally generated. Is there any way of doing
this (using .Net 1.1)?


Yup - just us "throw;". If you do "throw e;" you get a new stack trace,
but you don't with plain "throw;".

Jon

Mar 30 '06 #2

P: n/a
Though you can only use 'throw' alone inside the catch block where the
exception was caught.

I'm not sure whether this will be possible with you 'squirrelling it
away'!?

Do you know the exact type of the exception in question? You could
potentially pass the caught exception to a new instance of itself as
the inner exception?

throw new WellKnownException(caughtException.Message, caughtException);

I'm not sure how crazy that idea is?! Obviously, if you're inside the
original catch block, just use throw as Jon suggests.

Josh
http://www.thejoyofcode.com/

Mar 30 '06 #3

P: n/a
Josh Twist <jo********@gmail.com> wrote:
Though you can only use 'throw' alone inside the catch block where the
exception was caught.

I'm not sure whether this will be possible with you 'squirrelling it
away'!?


Whoops - yes, I missed that part. As far as I know there's no way of
avoiding messing with the stack at that point.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 30 '06 #4

P: n/a

"Josh Twist" <jo********@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Though you can only use 'throw' alone inside the catch block where the
exception was caught.

I'm not sure whether this will be possible with you 'squirrelling it
away'!?

Do you know the exact type of the exception in question? You could
potentially pass the caught exception to a new instance of itself as
the inner exception?

throw new WellKnownException(caughtException.Message, caughtException);

I'm not sure how crazy that idea is?! Obviously, if you're inside the
original catch block, just use throw as Jon suggests.

the situation I wanted to achieve was that the caller would always "see" the
original exception that caused the error - regardless of what exception
class that was, even though I have had to go through some hoops to preserve
it.

however, it looks like this is not an option, so I think the best option is
to just wrap it in one of my own making

Andy
Josh
http://www.thejoyofcode.com/

Mar 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.