Sunday, July 24, 2011

Clean Up Existing Checked Exceptions

 

Once in a while you’re having to work with an API that requires Checked Exceptions.  If you have a chance to work with the API, do you have to stick with them forever?  No! 

Do you have to remove them?  No!

Simply change the exception to derive from RuntimeException instead of Exception.

Example:

From:

	public class DbException extends Exception {
/* ... */
}


 


To:

	public class DbException extends RuntimeException {
/* ... */
}

 


Done!



  1. Now all new code doesn’t have to write do-nothing catch blocks which unfortunately is all too common in business code.
  2. It doesn’t break a bit of old code that was running.

 


Simple, effective, and doesn’t require a complete rewrite of existing code base just to save yourself new code pain.


P.S.  This is also an effective way to set up a release where an exception is deprecated as well.

No comments:

Post a Comment