I've seen here many general questions about the difference between Exception
and Throwable
. I know the difference, and I have a more specific question.
I'm writing a library that binds and runs together several user-supplied pieces of code. If one of the pieces fails, the whole computation is discarded. In order to keep resource usage clean, users can also supply finalizers that are run when such an event happens. The patterns is something like this:
try {
// process ...
} catch (Exception ex) {
runRegisteredFinalizers();
throw ex;
}
My question is: Should I intercept and rethrow just Exception
s like above, or should I also intercept Throwable
s? If an Error
occurs, is there any chance that
- JVM will recover? (So is there any point running finalizers?)
- JVM will be in such a state that it's actually possible to run them?
Also, when running finalizers, I catch and ignore their exceptions so that other registered finalizers have a chance to run, something like:
try {
finalizer.run();
}
catch (Exception ex) {
log.error("Exception in a finalizer", ex);
}
Again, should I intercept just Exception
s, or also Throwable
s? Ignoring and not rethrowing Error
s seem more problematic.