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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:48:37+00:00 2026-06-06T09:48:37+00:00

I need to catch two exceptions because they require the same handling logic. I

  • 0

I need to catch two exceptions because they require the same handling logic. I would like to do something like:

catch (Exception e, ExtendsRuntimeException re) {
    // common logic to handle both exceptions
}

Is it possible to avoid duplicating the handler code in each catch block?

  • 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-06T09:48:39+00:00Added an answer on June 6, 2026 at 9:48 am

    Java 7 and later

    Multiple-exception catches are supported, starting in Java 7.

    The syntax is:

    try {
         // stuff
    } catch (Exception1 | Exception2 ex) {
         // Handle both exceptions
    }
    

    The static type of ex is the most specialized common supertype of the exceptions listed. There is a nice feature where if you rethrow ex in the catch, the compiler knows that only one of the listed exceptions can be thrown.


    Java 6 and earlier

    Prior to Java 7, there are ways to handle this, but they are inelegant and have limitations.

    Approach #1

    try {
         // stuff
    } catch (Exception1 ex) {
         handleException(ex);
    } catch (Exception2 ex) {
         handleException(ex);
    }
    
    public void handleException(SuperException ex) {
         // handle exception here
    }
    

    This gets messy if the exception handler needs to access local variables declared before the try. And if the handler method needs to rethrow the exception (and it is checked) then you run into serious problems with the signature. Specifically, handleException has to be declared as throwing SuperException … which potentially means you have to change the signature of the enclosing method, and so on.

    Approach #2

    try {
         // stuff
    } catch (SuperException ex) {
         if (ex instanceof Exception1 || ex instanceof Exception2) {
             // handle exception
         } else {
             throw ex;
         }
    }
    

    Once again, we have a potential problem with signatures.

    Approach #3

    try {
         // stuff
    } catch (SuperException ex) {
         if (ex instanceof Exception1 || ex instanceof Exception2) {
             // handle exception
         }
    }
    

    If you leave out the else part (e.g. because there are no other subtypes of SuperException at the moment) the code becomes more fragile. If the exception hierarchy is reorganized, this handler without an else may end up silently eating exceptions.


    Note: if SuperException in the above is Exception you will either eat (all) unchecked exceptions or find that you are rethrowing an exception with the signature Exception. Both of those things will be problematic.

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

Sidebar

Related Questions

Code: catch (IOException e) { LOGGER.error(IOException exception happened); //now need throw again the same
whenever we need to exception handling, we will surround with try/catch block, but when
I need to catch two different swipping gestures using UISwipeGestureRecognizer (for example, UISwipeGestureRecognizerDirectionRight and
I need to catch that exception but I can't figure out which one it
I need a little help creating a catch-all error handling page in my ICEfaces
I've never had the need catch more than one exception at a time before.
I need to catch or filter a number from string example: var Mystring =
I need to catch segmentation fault in third party library cleanup operations. This happens
I need to catch swipe-action only on special area. But debugger tells unrecognized selector
I have gtk.Window and I need to catch closure. I need to close the

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.