I was going through some Exception Handling concepts in Java and came across the concept of final rethrow. I read the following article-
https://baptiste-wicht.com/posts/2010/05/better-exception-handling-in-java-7-multicatch-and-final-rethrow.html
Here, its written-
And the second improvement is a little more complicated. Imagine that you want to catch all exceptions, make several operations and then rethrow it. The code isn't hard to make, but the big problem is that you must add a throws clause to your method signature to manage the new exception launched by your code and this is not the objective. Now, you can do that without adding an exception throws clause :
// some code
} catch (final Throwable ex) {
// some more code
throw ex;
}
Using the final keyword it allows you to throw an exception of the exact dynamic type that will be throwed. So if an IOException occurs, an IOException will be throwed. Of course, you have to declare the exceptions not caught. You throws clauses will exactly the same if you use the code (in //some code) without catching anything but now you can do something if that happens.
Can somebody please explain what exactly does this mean and what purpose does it solve?