Cool or stupid? Catch (Exception [NamingException, CreateException] e)

I wrote code, and I notice a pattern in exception handling that made me think:

try{ // do stuff... throws JMS, Create and NamingException } catch (NamingException e) { log1(e); rollback(); doSomething(e) } catch (CreateException e) { log1(e); rollback(); doSomething(e) } 

Where a JMSException will handle some of them on the stack.

It would be simple to write:

 try{ // do stuff... throws JMS, Create and NamingException } catch Exception[NamingException, CreateException] e) { log1(e); rollback(); doSomething(e) } 

instead of putting it in a helper method:

 try{ // do stuff... throws JMS, Create and NamingException } catch (NamingException e) { helper_handleError1(e) } catch (CreateException e) { helper_handleError1(e) } 

Note that I want to distribute the stacktrace of the original JMSException, and I don't feel like creating a new JMSException with the third catch clause :)

Any gestures? Is this an extreme situation that will only contaminate Java syntax or just a cool thing to add?

+4
source share
8 answers

They are considering an extension of this type for Java 7.

See: http://tech.puredanger.com/java7#catch

+5
source

While we are compiling the syntaxes, here is how I would like to see it:

 try { // do stuff ... } catch (NamingException e) catch (CreateException e) { log1(e); rollback(); doSoemthing(e); } 

Similar to passing a switch statement or C # using . Of course, the problem here is that the variable e is declared twice, but I think something could be developed.

+4
source

I would like it to be possible to match patterns in the exception type as a syntactic add-on. Sort of

  try {
   ...
 } catch ((IOException &&! FileNotFoundException) || IllegalArgumentException) {
   ... handle it
 }
0
source

I suggest using the "Execute Around" idiom.

0
source

Try the following:

 try { ... } catch ( Exception e) { if typeof(e) not in ('MyException', 'SpecialException') { throw e } doSomething() } 

(pseudo code)

0
source

Firstly,

 } catch Exception[NamingException, CreateException] e) { 

missing a '(' char.

Secondly, why is "Exception [NamingException, CreateException] e" and not just "[NamingException, CreateException] e"?

The idea may be nice, but it needs polishing. For example, suppose I declare the class "MyException" with the function "doYellow ()" and "OtherException" with the function "doYellow ()". Is such a function allowed (given that there is no "doYellow ()" function in the superclass). If yes, is it permissible if "doYellow ()" was declared on only one of them? Is that the “Exception” before the [] characters? Also, suppose there is this declaration:

 void doYellow(NamingException e); void doYellow(CreateException e); 

(note that these are different functions) is it allowed to be called?

You really need to provide more details. However, this may be useful in some cases (this is not rare)

0
source

Another way to do it.

Convert the low-level exception to your own high-level exception and handle it.

 try{ try{ // do stuff... throws JMS, Create and NamingException } catch (NamingException e) { throw new MyException(e); } catch (CreateException e) { throw new MyException(e); } } catch (MyException e) { // something on e or e.getCause(); } 
0
source

Why not just

 try { // do stuff... throws JMS, Create and NamingException } catch (JMSException e) { if (e instanceof CreateException || e instanceof NamingExcption) { log1(e); rollback(); doSomething(e); } else throw e; } 
0
source

All Articles