Well here I’ve seen this in API and other related questions:
try {
} catch (IOException e) {
throw new SampleException("Other IOException", e);
}
Which calls a new exception.
But now I have seen this example which calls itself. I’m trying to write the documentation and it seems it calls itself again.
try {
statements;
}catch(TheException e) {
//perform operations before exits;
throw e;
}
Re-throwing the exception so that other handlers get a chance to
process the exception
I guess we can not add one more TheException after our catch exception! So in this case who is the other handler? I tested it and I realize even codes after catch clause are not compiled!
Let’s say you have two methods.
First one:
Second one:
See how method B calls method A. The exception raised originally inside method A is handler by method A itself, but once this is done, method A decides to throw the exception so other handlers get a chance to process the exception. Other hander as method A. Notice that method A can be in a different class, in a different layer.
For method A, throwing an exception is a nice way to inform method B that something happened.