I am trying to write an exception handler class that handles various exceptions in a flow. The business flow throws a lot of exceptions and the handler has methods that take all these exceptions as arguments and do the desired processing.
The behavior I am unable to understand is that when in the business flow I catch Exception only (as in not the specific ones) and then pass on this caught exception instance to the handler, only handle(Exception) is called and not the specific handler method destined for the specific exception. Following code snippet will explain my confusion.
public class Scrap {
public static void main(String[] args) {
try {
new Handler().handle(new BException());
throw new BException();
} catch (Exception e) {
new Handler().handle(e);
}
}
static class Handler {
public void handle(AException e) {
System.out.println(e.getClass());
System.out.println("AAE");
}
public void handle(BException e) {
System.out.println(e.getClass());
System.out.println("BBE");
}
public void handle(Exception e) {
System.out.println(e.getClass());
System.out.println("E");
}
}
static class AException extends Exception {
private static final long serialVersionUID = 1L;
}
static class BException extends Exception {
private static final long serialVersionUID = 1L;
}
}
The output is:
class Scrap$BException
BBE
class Scrap$BException
E
If I add another catch block as:
try {
new Handler().handle(new BException());
throw new BException();
} catch (BException e) {
new Handler().handle(e);
} catch (Exception e) {
new Handler().handle(e);
}
then the output is:
class Scrap$BException
BBE
class Scrap$BException
BBE
Why is the call to Handler.handle NOT going to the specific method with the specific exception in the first case.
Another thing to note is that if I add a cast on the first code snipped like
try {
new Handler().handle(new BException());
throw new BException();
} catch (Exception e) {
new Handler().handle((BException)e);
}
the output is as expected (same as the second snippet)
I am sure this behavior is intended, I just need pointers to that documented behavior, also the problem that I have is, my business flow throws ~30 exceptions and because of this behavior I have to write 30 separate catch blocks.
This is about static vs. dynamic binding in Java. The only position where you can leverage dynamic dispatch (polymorphism) is before the dot in a method call. No arguments that go inside parentheses are subject to dynamic dispatch and the compiler chooses a definite method signature, which cannot change at runtime, based on the declared (static) types of expressions in the argument list.
This is a fundamental feature that defines the kind of language Java is: it is a single dispatch language, just like most other OOP languages. Multiple-dispatch OOP is a completely different model. For example, in that case the basic picture that everyone assumes about OOP, of classes declaring their methods, falls apart: a method does not belong to any particular class and is a separate entity. In your particular case, the method
handlewould as much belong to each Exception type as to the Handler type and there wouldn’t be much sense in declaring it inside Handler.