Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How convince other developers not to ignore Exceptions?

Recently I encountered a bug in an application I took over from another developer. I debugged for the reason and over an hour later I realized, that the problem wasn't the code producing the exception, but some code executed before this returning wrong data. If I dived into this, I encountered the following:

try {
  ...
} catch (XYException e){}

If the Exception would have been propagated (a change I did), I would have found the reason for the bugs in a few minutes, as the stacktrace had pointed me to the problem. So how can I convince other developers to never catch and ignore exceptions in this way?

like image 291
Mnementh Avatar asked Mar 16 '10 12:03

Mnementh


3 Answers

Simple rule of thumb: catch exceptions if and only if you have a meaningful way of handling them. Do whatever you need to do at your workplace to propagate this simple rule.

By utilizing tools such as PMD, you can even enforce this in the development environment of all your developers. EmptyCatchBlock (first rule under Basic Rules) is a rule that does exactly what you need. You have some more out-of-the-box rules for exceptions if you need better control on exception handling.

Nevertheless, in my experience, enforcing the use of tools such as PMD is never a substitute to proper development practices and developer education.

like image 124
Yuval Adam Avatar answered Sep 19 '22 07:09

Yuval Adam


Start BadHumour:

Add a silent critical exception into the code that causes the application to shut down, and
let them sit and cry trying to find it for about an hour - Then educate

End BadHumour

The simple rule is simple: Use exceptions for exceptional circumstances. Anything else is silly and wasteful. Compare swallowing of exceptions to eating candy coated razor blades. They may taste nice now, but wait until they start to be digested. (Coding Test system vs. Debugging Live system)

like image 33
Kyle Rosendo Avatar answered Sep 18 '22 07:09

Kyle Rosendo


Simple point - have the lead developer declare that. Catching an exception and swallowing it without reason is equal to sabotage and should lead to recursion against the developer (i.e. talk i nHR about his attitude).

As Yuval said, catch exceptions only if you actually do something sensible. let things bubble up if you dont know what or how to handle them. POSSIBLY wrap them in other exceptions IF expected (i.e. a DAL may throw a DataLayerException so that higher up levels can handle all that stuff in one try/catch, but it would not handle something unexpected).

it is a ridiculous way to catch exceptions and swallow them.

like image 24
TomTom Avatar answered Sep 22 '22 07:09

TomTom