Exceptional Eclipse Tip

By default, when eclipse creates a try/catch block for you, you end up with something like this:

  try {
    doSomething();
  catch (EvilException e) {
    // TODO auto-generated catch block.
    e.printStackTrace();
  }

This is worse than useless, as it (effectively) covers up the exception1. A far better default choice is to wrap the checked exception in a RuntimeException if you don’t know what to do with it.

Thankfully, it’s fairly easy to arrange this in eclipse. Go to Preferences → Java → Code Style → Code Templates and edit the “catch block body” fragment. It should look like this:

eclipse-catch-block-template.png

I’m going to try and spread this around the office a bit. It should make for some slightly more robust code…

1 Please don’t remind me that it comes out on the console—people are very good at ignoring that.

Comments are Closed