Shiny Donkey!
Shiny Donkey! Shiny Donkey! Shiny Donkey!
                  Fake Banner Ads!  Mini-Sites!  
    New Shiny Donkey Posts  more >>
* BREAKING NEWS *  9 out of 10 Norse Gods recommend following the lemmings off a bridge instead of shinydonkey.com

.NET To err is human
[reply]   

03/31/06 12:32 PM EST
posted by JER email web

How do you handle Exceptions in your .NET code? I always catch exceptions I'm expecting (like a "directory not found" error) and try to resolve it without bothering the user. If it can't be resolved, I write the message & stack trace to an error log, then rethrow the Exception to allow it to bubble up.
Another "developer" and I had an argument recently about this practice, mainly because of critical comments I made about his code. Specifically, he uses the following code as a stub for all methods:

private void ExampleMethod(){
   try{
      //some code here
   } catch {
      throw;
   }
}

So basically he catches all errors and then rethrows them immediately.  I asked him what logic was behind that design choice and he told me that he doesn't trust the .NET Framework to handle all errors, so it's best to follow this practice.  Also, this will insure that errors bubble up to the caller.  Apparently he adapted it from someone he used to work with.

I have a few problems with this argument...

  1. If you "don't trust" the .NET Framework to handle Exceptions, why do you trust it to catch & rethrow them??
  2. A naked "catch" block will catch non-CLS compliant code, which can only be introduced via COM or some really sketchy manual compilation.  So if your method is calculating the volume of an egg and does not call any 3rd party dlls, why are non-CLS compliant exceptions a concern?

We had this e-mail exchange:

E-mail #1

Ken,
Specifically, this Microsoft recommendation seems to back up my main argument for not re-throwing an Exception

"Use Inner exceptions (chained exceptions). However, do not catch and re-throw exceptions unless you are adding additional information or changing the type of the exception."

Jerry

E-mail #2

Jerry, that's true I found out about it, but it look so unstructured, it is what's in the framework, less work for us.  However look at it in terms of distributed environment, this won't work.  Example Client.A() call web service B(), since B() is ws business layer with passive exception handling, when error occurred in datalayer ws is using error wont bubble up to the client.  It's error accessible in local framework.


Anyone else want to weigh in on this?


References
Best Practices for Handling Exceptions
.NET Framework Developer's Guide : Exception Handling
Unexpected Errors in Managed Applications

 


[reply]   

03/31/06 04:19 PM EST
posted by Blearns email web

You're right. He's wrong. Plain and simple.

You are following the exact MS best practices, and his "non-bubble" example doesn't sound correct to me, I haven't tested but I'm rather certain it will bubble. (And I'm rather certain I've seen it bubble before on a client's web service)

 

Name

 registered? log in!

E-mail (optional)

Website (optional)

  

To ensure security, this site requires unregistered users to enter a verification code:
 
Your code is:  
Enter Code:   

Note: Only registered ShinyDonkey.com users can post images. Only administrators can delete images.

  



"And remember, a shiny new donkey for whoever brings me the head of Colonel Montoya..."
e-mail webmaster