1

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.

1

4 Answers 4

3

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 handle would as much belong to each Exception type as to the Handler type and there wouldn't be much sense in declaring it inside Handler.

2

Why is the call to Handler.handle going to the specific method with the specific exception in the first case.

In the first case, you're calling it with a value with a compile-time type of BException:

new Handler().handle(new BException());

In the second case, you're calling it with a value with a compile-time type of Exception:

catch (Exception e) {
   new Handler().handle(e);
}

Basically, you need to be aware that overloading (the method signature chosen) occurs at compile-time... it's only the implementation of that signature (up and down the inheritance hierarchy of the target of the method call) that occurs at execution time.

1

This behaviour is because in your catch-block, the e variable has type Exception. You would need to do something like this to change the handle() called based on the Exception type:

public static void main(String[] args) {
    try {
        new Handler().handle(new BException());
        throw new BException();
    } catch (AException e) {
        new Handler().handle(e);
    } catch (BException e) {
        new Handler().handle(e);
    }
}
3
  • I understand what you are trying to recommend, and that's the way it's going to be, but shouldn't java evaluate the type of arguments a runtime before calling a method. I mean if there are multiple target methods for a call, shouldn't java call the most specific one? Like it does in catch blocks. Oct 28, 2012 at 21:07
  • @RaviSanwal: It's difficult to answer what Java should or shouldn't do, I can only tell you that this is the way it is implemented. You would have to get hold of the guys who wrote the JVM to get a good answer why.
    – Keppil
    Oct 28, 2012 at 21:10
  • Ya I get it, I'll invite James Gosling for a dinner and ask him. ..just kidding. But I did understand the problem and behavior, thanks Oct 28, 2012 at 21:27
1

The compiler will choose the method based on the compile-time type of the variable. You can choose the proper method at runtime via instanceof.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.