Rethrowing exceptions

C

Coder Guy

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
 
A

Alberto Poblacion

Coder Guy said:
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;".
 
C

Coder Guy

I think you missed the point. The exception is saved and rethrown on
another thread, and therefore, that won't work for me...
 
J

Jon Skeet [C# MVP]

Coder Guy said:
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?

Are you sure that Control.EndInvoke *does* handle this? Does it appear
to replace the stack trace with the original one?
 

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