More Never, Never, Never

Published Wed, Jun 8 2005 8:14 | William

I think a while back I posted something that you should Never ever ever ever ever do in respect to exception handling.  Well, my friend came across another one which is even better than the last one. He only gave it three Never's but I suspect he meant to give it more - I think it warrants five. If the pure ugliness of what's going on here doesn't bite you square in the face - then it's definitely time to polish up those Exception handling skills.

An old theory of mine is that code that's PURELY terrible is not a function of simple ignorance or idiocy.  You see, if it were ignorance, you'd tend to see the stuff get better in waves or there'd be some randomness to it.  If it was idiocy, it would be purely random.    But when you see something really ridiculous, done in a very methodical fashion--- oh no, that's almost always ignorance + ego

 

catch(System.Data.SqlClient.SqlException sqlExc)
{
throw sqlExc;
}
catch(Exception exc)
{
System.Exception ex = new System.Exception("SomeClassLibrary.SomeUser.ValidateUser Caught Null Reference Exception",exc);
throw ex;
}
Filed under:

Comments

# William said on June 8, 2005 8:33 AM:

if Never's are a count then I get about 3 maybe 4... if it's qualitative, well depends on what the Never.Max is ;)

1. rethrow by ommitting the exception. Catching it and rethrowing is oaky though, often useful when debugging.
2. if expecting a null reference excpetion, than sepcifically catch only that type
3. rethrow instead of inheriting if you aren't adding any useful information
4. don't put the type name inside a string literal as this may change and won't be caught by most analysis tools
5. (optional) consider localising informtation strings

That's 4, maybe 5, but if I follow the advice of 1 and 2, I probably wouldn't get to 3. Am i missing the obvious ?


# William said on June 8, 2005 8:40 AM:

Dude - nice CATCH! You get 20 extra bonus points for the #5. I didn't think of it but damn straight there.

2 is the million dollar one thow - and catching System.Exception but logging NullReferenceException is going to cause someone else to waste a lot of time tracking down the problem b/c they'll probably see it and spend time looking in the caller for the null before checking out this routine and realize what happened.

4- is another good point - and one I didn't even really think of for the reason you stated.

I would also throw in that catching System.Exception at this level is bad. Too much can happen that has Nothing to do with this routine.

You definitely pass the NEver Never test w/ flying colors.

# William said on June 16, 2005 7:01 PM:

Bill - you would've thought of Bill McCarthy's #5 if you've used FxCop enough ;-) it's got a picky default rule about localizing string literals.

I'd add that in the first catch block the two 'sqlExc' don't need to even be there. just this would suffice:

catch(SqlException)
{
throw;
}

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications