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?
They are considering an extension of this type for Java 7.
See: http://tech.puredanger.com/java7#catch
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With