Java – how do you convince other developers not to ignore exceptions?
I recently encountered an error in an application taken over by another developer I debugged the reason. After more than an hour, I realized that the problem was not the code that generated the exception, but that some code was executed before returning the wrong data If I do, I encounter the following situations:
try { ... } catch (XYException e){}
If the exception propagates (a change), I will find the cause of the error in a few minutes because stacktrace points out my problem So how can I convince other developers that they will never catch and ignore exceptions in this way?
Solution
Simple rule of thumb: catch exceptions if and only if you have a meaningful way to handle them Do whatever you need to do to spread this simple rule
By using tools such as PMD, you can even implement it in the development environment of all developers Empty catch block (the first rule in the basic rule) is a rule that exactly meets your needs If you need better control over exception handling, you also need more out of the box rules for exceptions
However, in my experience, the use of tools such as PMD can never replace appropriate development practice and developer education