Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

Rethrowing exceptions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Coder Guy - 06 May 2007 23:26 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.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.