Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Java 7 introduced a great new feature where, if multiple exceptions are possibly thrown by a single piece of code and if the catch handles each of those exceptions in the very same way, you can simply have one exception handler, like this:

    Code Block
    languagejava
    try {
    	String.class.newInstance();
    } catch (IllegalArgumentException | NoSuchMethodException ex) { // i know these aren't right, but you surely get the use of the language feature now, right?
    	LOG.error(ex);
    	throw new RuntimeException(ex);
    }

Suggestions:

  • Every exception should be logged. Don’t silently swallow it.
  • Don’t discard any exception information, but add more information to an exception. That is, even if you only catch and re-throw, add some of the relevant method parameters or instance variables to the error message.

...