Lance's Whiteboard

Random scribbling about C#, ASP.NET, Sql Reporting, etc.

News

BlogMailr Enabled




Sponsored Ad
Sponsored Ad

Blogs I Read

Developer Sites

Googling, etc...

MSDN Resources

Tutorials & Reference

Try/Catch annoyances

The Try/Catch block is the source of many of my pet peeves, here is one of my favorite petty annoyances:

Eating Exceptions:

try
{
  … do something meaningful
}
catch(SomeException ex)
{
   // eat exception
}

This is an example of a block that is intended to eat exceptions (for the sake of argument, lets assume there is a valid reason).

[remark: i'll save my lecture on the pitfalls and evil of eating exceptions for another day]

I see this in many projects.  In and of itself the code isn’t bad, depending upon its usage. I just dislike seeing the “ex” variable hanging out there.  The variable name is redundant and generates the warning The variable 'ex' is declared but never used.”  Instead, just omit the variable name, and keep the exception type filter like this:

 
try
{
  … do something meaningful
}
catch(SomeException)
{
   // eat exception
}

 
More than anything else, what bothers me is the REASON why the “ex” variable was left-in.  Usually, developers leave it so they can see the exception when stepping-through code in the VisualStudio debugger.   

 I wonder if the following should be done:

 A)      Make the Visual Studio debugger treat catch blocks differently and always step "onto" that line and give you a smart tag with the exception instance being caught/eaten.

B)      Add a new keyword to C# (and other languages) to make “eating” exceptions more explicit.  Ex:   try/eat or try/catch/eat instead of try/catch

[update: pruned this post to avoid conveying the sentiment that eating exceptions is a good thing (thanks to Paul for making me smack myself upside the head)]
Posted: Jan 15 2007, 05:16 PM by CodeSniper | with 12 comment(s)
Filed under:

Comments

Wilco Bauwer said:

Try using the $exception variable in the debugger. That way you don't need the exception variable in your code that causes a compiler warning.

# January 15, 2007 6:35 PM

CodeSniper said:

Great tip!

But are 99% of developers going to automatically know to open the Immediate window and type that?

Ideally, the Dev tools & language should make it easy for the developer to accidentally "do the right thing", so the point I was making is that this is one area where the debugger and language encourage a bad practice.  

As we all know, developers are lazy, and if they are repeatedly debugging the same code, at some point its quite likely (esp for a junior dev) that you may see the redundant variable declaration...

# January 15, 2007 6:52 PM

Wim Hollebrandse said:

Obviously the other annoyance that springs to mind and that I'd expect you to mention when I was reading your post is a rethrow of the caught exception like:

try

{

 // whatever...

 // several lines of code

}

catch (Exception ex)

{

 // some logging

 throw ex;

}

Which destroys the original stack trace of where the exception originated in the first place.

Instead, either simply use "throw;" or wrap the exception with your own exception class and attach the original exception as an inner exception in the appropriate overloaded exception constructor.

# January 15, 2007 7:07 PM

Paul Ballard said:

I couldn't disagree with your post more. :-)  Eating exceptions is evil, pure and simple.  Either you can do something meaningful with the exception or you can't, in which case catching it is tantamout (sp?) to smacking baby seals with large clubs.  Just don't do it.

Any unhandled exception that occurs during debugging will trigger a prompt to the developer.  If said developer needs the information contained in the exception then that information is in the debugger visualization tools or the $exception variable.  

Developers who leave Catch (Exception ex) or for those VB devs out there Catch(ex as Exception) with no actual code in the Catch block are simply being lazy.  But what's worse they are sweeping actual bugs under the rug because the defect that caused the exception causes no immediate feedback and therefore is often not stomped out in development when it should be.

Instead of adding constructs to reinforce their laziness how about if code completion for the try statement added a Finally instead of a Catch.  Make the developer explicitly add the exception handling in methods.  Then perhaps make application level exception handling smoother (although it's pretty nifty as it is).  

Paul

# January 15, 2007 7:14 PM

Wilco Bauwer said:

Actually, it's relatively discoverable if you use the Locals window. Additionally, if you hover the Exception line, VS opens a 'smart' window with all the information about your exception (much like your first suggestion).

Personally I don't like the idea of having an "eating" statement, because 1) rarely is there a good reason to eat an exception in the first place 2) because of 1, I don't think it's worth adding a construct which will likely encourage people to do The Wrong Thing (tm) and give handling the exception properly less thought.

# January 15, 2007 7:16 PM

Gabriel Lozano-Moran said:

'Eating' exceptions is one of the bad practices and the day that Microsoft decides to add this to the C# language specifications I will walk over to J2EE. Bug hiding is never, but truly never a good idea.

# January 16, 2007 12:10 AM

Uwe said:

I _never_ silently eat exceptions. The least thing I _always_ do is to log exceptions.

# January 16, 2007 12:31 AM

John said:

So you don't like "ex" hanging out there (when you can name it whatever you like) but instead want extra, unnecessary syntax? did you know you can have multiple catches each with a different type of exception?

# January 16, 2007 9:53 AM

CodeSniper said:

Wim - Agreed.  Except the "throw ex" is more than just a pet-peave, its down right evil!

Paul - I agree that eating exceptions should not be encouraged.  Perhaps the "eat" keyword idea goes a bit too far.  

Wilco - I agree that the unused exception declaration shouldnt be neccessary, and developers should seek other ways, but it is still quite a common practice.  The quest is, how do we help the developer to do the right thing?  Education is fine, but it would be nice to take it one step further.

Uwe - That is the policy everyone should follow.  Basically, stick to the "Dont catch an exception unless you can handle it."

John - My point wasnt about the "ex" variable name, it was about the fact there is any variable name at all, when the exception isnt referenced.   Yes, I'm aware of exception filters, but I'm not sure how that applies here.  All that really means is that you might have several instances of unreferenced declarations - which is exactly what I'm trying to discourage.

# January 16, 2007 10:19 AM

CodeSniper said:

Perhaps a simpler solution would be to have the compiler issue a warning on any block that results in eaten exceptions.  Then, for those of us with "Treat Warnings as errors" enabled, we can enforce review of this code before any release.   Otherwise, the "The variable 'ex' is declared but never used." warning might be overlooked and the eaten exception go undetected.

# January 16, 2007 10:32 AM

Wim Hollebrandse said:

Lance - I don't think swallowing exceptions is less evil than "throw ex;"...

I would say they're at least on a par in terms of 'evilness'!

# January 16, 2007 1:02 PM

Ian Cooper said:

Using warning level 4 and treat warnings as errors will solve the declaration without being used problem for you.

http://iancooper.spaces.live.com/blog/cns!844BD2811F9ABE9C!302.entry

# January 17, 2007 5:03 AM