Thursday, August 2, 2012

Anti pattern : Ignoring Exceptions

Did you ever find out that your app didn't do what it was expected to do ? But no clue whatsoever about the cause. You might stumbled upon 'Ignored Exceptions' antipattern. Or worse.. a special case of this antipattern is 'whatever passed the acceptance test' mindset that causing the programmer to code in such way.
The most primitive example of this antipattern is :
[this one-liner is an example from Visual Basic]

In Java programming language, an example for Ignored Exceptions antipattern :

   try {
  ... some code ..
  --- more code ---
  catch (Exception ex) {

Note  that the exception is blank. It silently ignores any errors.
Yes, there are cases where this sort of code would be hard to avoid, but in most cases better alternatives exist. Such as :
- using Log4j to log the error
- converting the exception to a message that could be understand by the user
At the minimum , the error should be written to the console, so troubleshooting can be done about the error :
  catch (Exception ex) {
     ex.printStackTrace() ;

No comments: