Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I was writing some code, and I notice a pattern in the exception handling that got me thinking:

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 JMSException would be handle some where up in the stack.

Would it be to just write:

try{

        // do stuff... throws JMS, Create and NamingException
} catch Exception[NamingException, CreateException] e) {

        log1(e);
    rollback();
        doSomething(e)
}

instead of putting it in tu a helper method:

try{

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

        helper_handleError1(e)
} catch (CreateException e) {

        helper_handleError1(e)
}

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

Any toughs? Is this an extreme situation that would only pollute the syntax of Java, or just a cool thing to add?

like image 719
João Avatar asked Jan 24 '23 23:01

João


2 Answers

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

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

like image 150
Darron Avatar answered Jan 26 '23 14:01

Darron


As long as we're making up syntaxes, here's how I'd like to see it:

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

Similar to the the fallthrough of a switch statement or C# using block. Of course, there's a problem here with the variable e declared twice, but I think something could be worked out.

like image 33
Joel Coehoorn Avatar answered Jan 26 '23 13:01

Joel Coehoorn