I was trying out a demo on Exception handling in controller using an interception @Finally.
I made an intentional NullPointerException in the controller and tried to catch the exception in the @Finally block.
When I tried this then it works:
@Finally
static void log(Throwable e) {
if( e == null ){
Logger.info("Inside FInally: Action call was successful");
} else{
Logger.info("Inside Finally: Action call failed", e);
}
}
But if I try this the it does not work:
@Finally
static void log(NullPointerException e) {
if( e == null ){
Logger.info("Inside FInally: Action call was successful");
} else{
Logger.info("Inside Finally: Action call failed", e);
}
}
Again if I consider the @Catch interception then both the following works:
@Catch(value = Throwable.class , priority = 2)
public static void te(Throwable throwException){
.....
}
@Catch(value = NullPointerException.class , priority = 1)
public static void npe(Throwable throwNullPointerException){
.....
}
Why is the second scenario not working in @Finally interception? Is it a compulsion to use only Throwable superclass for any kind of exception rather than using any specific subclass exception?
Please let me know about this.
The online developer guide does not explicitly say that you cannot use specific Exception type. But considering the idea behind this @Finally intercetpion, I guess you are right; that is, @Finally only takes Throwable.
Let’s think about it from this way, what if you have multiple catch statements in the method, only one catch will be triggered if there is an exception thrown. In this case, it would be confusing if the argument is NPE but the DBConnectionTimeout(for instance) is actually thrown. Therefore there is noway that your interceptor can work with other type of excpetion, nor you can have multiple @Finally interceptors.