Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8742463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:30:18+00:00 2026-06-13T11:30:18+00:00

I am trying to write an exception handler class that handles various exceptions in

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:30:19+00:00Added an answer on June 13, 2026 at 11:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying ot write an integration test for one of my methods, that handles
I'm trying to write a linq to sql method that handles sorting, paging, and
I am trying to configure Exception Handling Application Block to write exceptions to Event
I am having trouble with XMLTextWriter.WriteStartElement throwing an exception: System.InvalidOperationException when trying to write
I m trying write code that after reset set up rrpmax as 3000. It
I am trying write a function that generates simulated data but if the simulated
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
I have a class with number of methods and want to have one exception
I'm trying to write a method that will return a Hibernate object based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.