0 votes
in Education by (1.3m points)
This question already has answers here:

Can I catch multiple Java exceptions in the same catch clause? (10 answers)

Closed 7 years ago.

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?

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.3m points)
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 problem, but they tend to be inelegant, and to 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!

Learn More with Blogmepost

blogmepost Online Test

NCERT Questions & Answers

  • NCERT Class 12 Maths Solutions
  • NCERT Class 12 Physics Solutions
  • NCERT Class 12 Chemistry Solutions
  • NCERT Class 12 Biology Solutions
  • NCERT Class 10 Science Solutions
  • NCERT Class 10 Maths Solutions
blogmepost Q&A

CBSE Questions & Answers Portal

  • CBSE Class 12 Maths Questions Answers
  • CBSE Class 12 Physics Questions Answers
  • CBSE Class 12 Chemistry Questions Answers
  • CBSE Class 12 Biology Questions Answers
  • CBSE Class 10 Science Questions Answers
  • CBSE Class 10 Maths Questions Answers
Blogmepost

Learn Math Formula, Revise Notes

  • JEE Crash Course Questions & Answers
  • NCERT Course Questions and Answers
  • CBSE Board Questions Bank
  • ICSE Board Questions Bank
  • Free Study Materials
  • Question & Answers Bank for All Classes
  • Text Book Solutions
  • Previous Year Question Paper
  • NCERT Book Solutions

  • NCERT
  • NCERT Solutions
  • NCERT Solutions for Class 12 Maths
  • NCERT Solutions for Class 12 Physics
  • NCERT Solutions for Class 12 Chemistry
  • NCERT Solutions for Class 12 Biology
  • NCERT Solutions for Class 11 Maths
  • NCERT Solutions for Class 11 Physics
  • NCERT Solutions for Class 11 Chemistry
  • NCERT Solutions for Class 11 Biology
  • NCERT Solutions for Class 10 Maths
  • NCERT Solutions for Class 10 Science
  • NCERT Solutions for Class 9 Maths
  • NCERT Solutions for Class 9 Science
  • CBSE Classes

  • CBSE Class 5 Questions
  • CBSE Class 6 Questions
  • CBSE Class 7 Questions
  • CBSE Class 8 Questions
  • CBSE Class 9 Questions
  • CBSE Class 10 Questions
  • CBSE Class 10 Maths Questions
  • CBSE Class 11 Commerce Questions
  • CBSE Class 11 Engineering Questions
  • CBSE Class 11 Medical Questions
  • CBSE Class 12 Commerce Questions
  • CBSE Class 12 Maths Questions
  • CBSE Class 12 Engineering Questions
  • CBSE Class 12 Medical Questions
  • BOARDS

  • CBSE
  • ICSE
  • NCERT
  • IGCSE
  • Andhra Pradesh
  • Bihar
  • Gujarat
  • Jharkhand
  • Karnataka
  • Kerala
  • Madhya Pradesh
  • Maharashtra
  • Punjab
  • Rajasthan
...