Categories
Uncategorized

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s