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 6202769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:47:47+00:00 2026-05-24T04:47:47+00:00

This is on Java 6 Can I have a common method to handle my

  • 0

This is on Java 6

Can I have a common method to handle my exceptions – so instead of doing this n times in each method

try {
    // Do something
} catch (XException e) {
    // Do something
} catch (YException e) {
    // Do something
} catch (ZException e) {
    // Do something
}

I have

try {
        // Do something
    } catch (Exception e) {
        handleAll (e);
    }

and method handleAll(e) does

if e.instanceOf(XException)

else if e.instanceOf(YException)

else if e.instanceOf(ZException)

Is there anything wrong with the 2nd approach?

Update:

My original question was about “centralizing the handling” in one place for both checked and runtime exceptions. The answers have pointed out I should avoid instanceof().

@aioobe’s idea looks very neat to me. Are there any negative opinions on that approach?

  • 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-05-24T04:47:47+00:00Added an answer on May 24, 2026 at 4:47 am

    There is one minor problem as I see it. Since you really want the handleAll method to rethrow any uncaught exception, it has to be declared throws Exception. This means that so does the methods that call handleAll.

    If X-, Y- and ZException are all RuntimeExceptions I see nothing wrong with this. (I may have overlooked something though, as this is the first time I’ve seen this approach.)

    To be sure that the instanceof approach behaves exactly as the catch clauses would, I would consider designing the handleAll(RuntimeException e) like this though:

    private void handleAll(RuntimeException e) {
        try {
            throw e;
        } catch (XException xe) {
            ...
        } catch (YException xe) {
            ...
        } catch (ZException xe) {
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this java string: String bla = <my:string>invalid_content</my:string>; How can I replace the
I have written a java annotation that looks like this: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) // can
How can I make this java generic cast ? public interface IField { }
Can anyone tell me what exactly does this Java code do? SecureRandom random =
OK - I know I can run my java process like this : java
Can you explain to me the output of this Java code? int a=5,i; i=++a
What is the maximum file size 32 bit java can access? Is this architecture
in java we can do this: public class A{ public static void main(String...str){ B
How can this line in Java be translated to Ruby: String className = java.util.Vector;
How is it that Java's BigDecimal can be this painful? Double d = 13.3D;

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.