I am implementing an asynchronous handler (similar to Control.EndInvoke) and
if the other thread throws an exception, I catch it and rethrow it on the
current thread. How can I rethrow the exception on the current thread while
maintaining the stack trace? The best I've come up with is encapsulating
the exception as an inner exception. I would prefer to just rethrow the
exception directly -- no need for an inner exception.
How does Control.EndInvoke handle this?
Thanks
Alberto Poblacion - 07 May 2007 07:22 GMT
>I am implementing an asynchronous handler (similar to Control.EndInvoke)
>and if the other thread throws an exception, I catch it and rethrow it on
>the current thread. How can I rethrow the exception on the current thread
>while maintaining the stack trace? The best I've come up with is
>encapsulating the exception as an inner exception. I would prefer to just
>rethrow the exception directly -- no need for an inner exception.
You can rethrow the current exception writing just "throw;" instead of
"throw myNewException;".
Coder Guy - 07 May 2007 07:35 GMT
I think you missed the point. The exception is saved and rethrown on
another thread, and therefore, that won't work for me...
> You can rethrow the current exception writing just "throw;" instead of
> "throw myNewException;".
Peter Bromberg [C# MVP] - 07 May 2007 12:11 GMT
Once you "throw ex" rather than "throw" you lose the stack trace. So,
providing the innerException as you described is probably the only way to do
what you want, unless you want to copy everything to a new exception object.
Peter

Signature
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
> I think you missed the point. The exception is saved and rethrown on
> another thread, and therefore, that won't work for me...
>
> > You can rethrow the current exception writing just "throw;" instead of
> > "throw myNewException;".
Jon Skeet [C# MVP] - 08 May 2007 19:40 GMT
> I am implementing an asynchronous handler (similar to Control.EndInvoke) and
> if the other thread throws an exception, I catch it and rethrow it on the
[quoted text clipped - 4 lines]
>
> How does Control.EndInvoke handle this?
Are you sure that Control.EndInvoke *does* handle this? Does it appear
to replace the stack trace with the original one?

Signature
Jon Skeet - <skeet@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