0

As to perform different logic upon the exceptions. As following:

catch (IOException e | IllegalArgumentException a) { e.doStuff(); a.doStuff(); } 
0

5 Answers 5

5

That doesn't make sense. When you use a multi catch, then you are implicitly saying: all of "these" exceptions should fall into the same bucket.

Of course, you can then do some instanceof if/else trees, but heck: the java way of doing that would be to have different catch statements for each one.

But, also of course, depending on context, it might be pragmatic to do something like

 catch(XException | YException | ZException xyOrZ) { log(xyOrZ); handle(xyOrZ); 

where handle() does some instanceof "switching".

Long story short: multi catch is a convenient way to enable an aspect (such as logging) that works for all exceptions. But it can get into your way regarding exception specific handling. You simply have to balance your requirements, and use that solution that your team finds to best fit your needs. To a certain degree, this is about style, and style questions are decided by the people working the code base.

Sign up to request clarification or add additional context in comments.

Comments

5

No, you can't do it that way. This is what multiple catch blocks are for.

The | form lets you combine different exception types in a single catch, but with a common variable for the exception. Its purpose is to allow unified handling of disparate exception types. But when you want disparate handling of disparate types, use multiple catch blocks.

Comments

3

In short - no. Multi-catch for exceptions has been introduced to join multiple catch blocks with same logic(like logging). So you want to do a reverse engineering here.

If you want to handle each exception in a different way, then use old notation:

catch (IllegalArgumentException a) { // handle } catch (IOException e) { // handle } 

Doing it inside of a multicatch block would require using instanceOf, which looks nasty and less readable:

catch (IOException | IllegalArgumentException a) { if(a instanceof IOException) { ((IOException)a).doStuff(); } else if(a instanceof IllegalArgumentException){ ((IllegalArgumentException)a).doStuff(); } } 

1 Comment

What language is your second code example? In Java it is instanceof (lower case O) and a (a IOException).doStuff(); looks quite unknown to me (and my jvc).
1

Yes, it is possible to catch multiple Exception in same catch block and perform different logic according to Exception caught.
For Example:

catch (IOException | IllegalArgumentException e) { if(e instanceof IOException){ //execute logic for IOException } else if(e instanceof IllegalArgumentException ){ //execute logic for IllegalArgumentException } } 

Comments

1

Since some time, you can use lambda-Expressions and switch over Exceptions:

import java.sql.*; public class ExceptionLambdaTest { public ExceptionLambdaTest (String param) throws IllegalArgumentException, SQLException { if (param.equals ("i")) throw new IllegalArgumentException ("bad a"); else if (param.equals ("s")) throw new SQLException ("no sql at all"); } public static void main (String args[]) { String param = "i"; if (args.length == 1) { param = args[0]; } try { new ExceptionLambdaTest (param); } catch (IllegalArgumentException | SQLException isex) { System.out.println ("Assuming some common code here."); switch (isex) { case IllegalArgumentException iae -> System.err.println ("i:> " + isex.getMessage ()); case SQLException se -> System.err.println ("s:> " + isex.getMessage ()); // case Exception e -> System.err.println ("Unexpected Exception:> " + isex.getMessage ()); default -> System.err.println ("Unexpected Exception:> " + isex.getMessage ()); } } } } 

I don't see an advantage over different catch blocks, except you would would have to do common stuff, which can be made before branching into one statement.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.