About Java Exceptions in SE 7

Posted by in Software

TL;DR: JDK 7 improves on exception handling (less code, base exception class…).

Base exception class

Reflective operations exceptions now have a base class ReflectiveOperationException. This allows you to to a global catch rather than one for each exception. Now you can write:

Note: Reflective operations are ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException.


Previously, you’d had to write code like this:

Now, you can write:

Woo-hoo! Less lines of code!

Final catch

Normally, when you re-throw form a catch, you only re-throw the caught exception (or a sub-type). IN Java 7, you have the option to declare the catch parameter as final:

This will actually process all exceptions of type MyException as well as all other uncaught exceptions in the block. This allows you to wrap code in a try/catch block, intercept, process and re-throw exceptions without affecting the statically determined set of exceptions thrown from the code (aka, simplified maintenance).

Note: The “statically determined set of exceptions” are exceptions that can be statically identified (i.e. defined by the throws clause on method definition.

A little experiment: If you find this post and ad below useful, please check the ad out 🙂