Skip navigation

Tag Archives: catch

Just came across this in a C code apple example:

do
{
  // stuff..
} while (false);

The code breaks the loop when an error occurs inside it. This is kind of like try/catch/finally, where you have a simple catch(…) all block that you jump to if you “break” the loop. It seems like a poor substitute for a function call however.

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.