Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello. I'm reading through the book called "CLR via C#". In Exceptions part, i have stumbled into this part i can't understand. Apparently the code below is how some programmers handle the issue of lost original exception point in unhandled exceptions:

C#
private void SomeMethod() {
 Boolean trySucceeds = false;
 try {
 ...
 trySucceeds = true;
 }
 finally {
 if (!trySucceeds) { /* catch code goes in here */ }
 }
}


I know finally block finishes first before Unhandled Exception pops up, but i don't understand how does it accomplish preserving original exception point. Any ideas?

Thanks in advance.
Posted
Comments
Herman<T>.Instance 31-Mar-15 3:13am    
Did you make a small app to test the situation? Did you debug the code? That should give you a learning curve in your question.
Sinisa Hajnal 31-Mar-15 3:41am    
Why not simply have catch with single throw statement which re-throws original exception with location preserved?

1 solution

If I recall the book correctly, that code is supposed to work around an issue in the way the CLR is implemented on Windows. Even though the CLR will know the full stack trace if you rethrow an exception this way;
C#
void SomeMethod() {
  try {
     // Do something that may throw
  }
  catch {
     // caught it, do something and then re-throw
     throw;
  ]
}

Windows will create a new stack frame from the re throw point. This isn't an issue if you catch and log the exception, as that will give you the correct stack trace, but if it crashes the application (because it's un-handled) and you need to use something like windbg to inspect the crash dump, the original originating point of the exception will be lost.

In most cases you don't need to worry about this, it's often better just to add an uncaught exception handler that logs the exception and it's stack trace, that's easier to use as a basis for trouble shooting than windbg (or at least, it often is).

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Sascha Lefèvre 31-Mar-15 4:04am    
I thought throw; would avoid exactly that problem (of a new stacktrace) in contrast to throw oldEx; ? :confused:
Fredrik Bornander 31-Mar-15 4:11am    
It does, but only as far as the CLR is concerned. The exceptions in the CLR (on Windows) are implemented using SEH (Structured Exception Handling), and it's that that's creating a new stack frame.
If you use it from within the CLR, it works as you thought, if you need to inspect a crash dump it works the other way.
Sascha Lefèvre 31-Mar-15 4:21am    
I guess I have some reading-up to do :) Thank you, my 5.
Fredrik Bornander 31-Mar-15 4:28am    
Probably not, in most cases you do not need to worry about this. Just re-throw using throw; and you'll be fine.

I managed to find the extract from Richter's book;
https://books.google.de/books?id=36tCAwAAQBAJ&pg=PT686&lpg=PT686&dq=Boolean+trySucceeds+%3D+false%3B&source=bl&ots=7j0BSnkwFV&sig=3ujzJWRYvpF6k-qvWiuxq2hDw6U&hl=en&sa=X&ei=QVoaVZeBPYv5atatghA&redir_esc=y#v=onepage&q=Boolean%20trySucceeds%20%3D%20false%3B&f=false
Sascha Lefèvre 31-Mar-15 4:40am    
Thank you very much, Fredrik!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900