Rethrowing of Exception in Java

Rethrowing of exception in java has improved starting from Java 7. We can now specify more specific exceptions using throws keyword.

Before java 7:

In the above method, the try block can throw either UserException1 or UserException2, this exceptions are caught in the catch block, here we add some statements and then throw a new exception.
Now if you wanted to specify the 2 exceptions in the Throws clause , earlier to java 7 this was not possible, hence you had to throw a new exception. This meant that the original exception was overridden by a new exception.

To handle this starting from java 7, you can Rethrow Exceptions with More inclusive Type Checking.

Using this you can now rethrow the original exception, that may be helpful for the end consumer of the method.

Reference

1. Java 7 – Rethrow Exceptions

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn