1

I'm been reviewing some production code, and it seems there are a lot of Try/Catches, with some logic in the Try, but an empty Catch.

Is this a normal practice?

Could you not just use an If statement?

It seems odd to use the Try/Catch if you're not going to Catch anything.

3 Answers3

4

A catchall statement that does nothing is an, unfortunately, common bad programming practice:

try
{
    DoSomething();
}
catch (Exception e)
{
    //empty
}

It has humorously been called Pokémon Exception Handling (#2), because "you just Gotta Catch 'Em All".

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
1

You are partially right.

An if-else though is no replacement for a try-catch. If Your code throws en exception ( willingly or unwillingly ), and You want either the code to proceed with processing, or just throw another exception upwards to caller,a try catch is the way to go. I do not see Your code in order to evaluate the rightness of try catch implementation. Could be helpful to show it.

icbytes
  • 1,831
  • 1
  • 17
  • 27
1

I have a story about a guy, who was the best programmer in the development team. Nobody could beat him, because all of his codes never ever threw any exceptions for many years.

When he quit and others began to review his codes, the horror story began.

He used try and empty catch for every logic in his code.

The development team had to rewrite his entire code base.