Skip navigation

I don’t have any real technical reason for hating exceptions. I simply hate exceptions in a stylistic sense.

Exceptions are a retroactive way of handling error conditions. They kind of say “Yeah try this, if that doesn’t work, then do that.”

try
{
  to parse the value out of the string
}
catch( ValueParsingException ex )
{
  not a valid value..?
}

Seems like a manager with a bad attitude. Like “Yeah try it, whatever, and if that doesn’t work, then do this other thing”. Sure, technically it’s very clear what the code is doing. But to me, (mostly because I’ve seen too many 200 line try/catch blocks), try/catch will always be a code structure very readily abused.

I generally hate people that say they will do one thing, then partway through they say “No, go back” and start doing something else. But that is exactly the language of a try/catch block.

Exceptions are like walking across the street without looking:

try
{
  to cross the street
}
catch( BusHitException busException )
{
  go to hospital?
}

I much prefer a series of rigorous if statements to try/catch blocks where possible. I’d much rather have code like this:

if( the string is valid )
{
  parse the value out of the string
}
else
{
  not a valid value.
}

So in other words, look before crossing:

if( there are no cars )
{
  cross the street
}
else
{
  wait
}

That would be more like a while loop, but you get the idea.

If statements say “I’m watching what is going on, this code knows what its doing.”

Sure, you can achieve the exact same ends with try/catch as with if/else. But try/catch all too often is used as sloppy code. I hate when exceptions are used in the generic “If something goes wrong then just throw that general error “Uh there was an exception” and printstacktrace.”

Have you ever seen one of those 200 line try blocks? Disgusting. People use them because they don’t know/they don’t care what is going wrong.

Try/catch can too easily be abused. That’s why I hate it as a programming construct.

Advertisements

One Comment

    • Vivek Rathi
    • Posted January 15, 2014 at 4:01 pm
    • Permalink

    yes! I totally agree with you. I was recently given an assignment at work, 600 lines of try block! For someone very naive and new to programming, it was a disaster. Oh, how much I hate try-catch now.


One Trackback/Pingback

  1. By boboboboisms | Bobobobo's Weblog on 27 May 2013 at 10:26 am

    […] Exceptions are a retroactive way of handling error conditions. They kind of say “Yeah try this, if that doesn’t work, then do that.” Exceptions are too easily abused. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: