in Education by
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
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!

Related questions

0 votes
    Which of the following classes can catch all exceptions which cannot be caught? (a) RuntimeException (b) Error (c ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these class is related to all the exceptions that can be caught by using catch? (a) Error (b) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
0 votes
    How to Catch multiple exceptions at once in C#?...
asked Jan 15, 2021 in Technology by JackTerrance
0 votes
    I'm not advanced programmer in SQL and maybe my question is silly, but I haven't found an answer ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    What are two classes which are present in the Test Package by default? Is it possible for the developers or testers to modify the same?...
asked Feb 7, 2021 in Technology by JackTerrance
0 votes
    Which of these keywords are used for the block to be examined for exceptions? (a) try (b) catch (c) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these keywords are used for the block to handle the exceptions generated by try block? (a) try ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these keywords are used for the block to be examined for exceptions? (a) try (b) catch ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
0 votes
    I have one weird requirement that in my existing app I have Text2Speech and for that, I have used ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    The method adopted by block cipher modes to generate unique ciphertexts even if the same plaintext is encrypted multiple times ... Vector (2)Random Keys (3)Padding (4)Blockchain...
asked Mar 19, 2021 in Technology by JackTerrance
0 votes
    I am able to modify my first spinner but not sure how to do the second one in java. Here is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
...