4

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?

8 Answers 8

5

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

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

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

Comments

4

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.

Comments

0

I would like if it would be possible to do some pattern matching on exception type as a syntactic addiotion. Something like

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

Comments

0

I suggest using the "Execute Around" idiom.

4 Comments

What is the "Execute Around" idiom?
I don't think that really fits his case; he wants at least one Exception to escape and propagate up the chain.
Greg: I don't see that in the original question, and I don't see that as a problem anyway.
0

Try this:

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

(pseudo code)

1 Comment

It looks good as pseudo code, but is not much practical when converted to real code ;(
0

In first place,

} catch Exception[NamingException, CreateException] e) { 

lacks a '(' char.

In second place, why "Exception[NamingException, CreateException] e" and not just "[NamingException, CreateException] e"?

The idea may be nice, but it needs some polishing. For example, suppose I declare "MyException" class with a function "doYellow()" and a "OtherException" class with a function "doYellow()". Would such function be allowed(considering there is no "doYellow()" function in the superclass). If so, would it be allowed if "doYellow()" was declared on only one of them? Is that what the "Exception" before the [] chars is for? Also, suppose there is this declaration:

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

(note that they are different functions) would it be allowed to be called?

You really should provide further details. However, it can be useful in some cases(it's not rare)

Comments

0

Another way to do.

Convert a 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(); } 

Comments

0

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; } 

1 Comment

Sometime you may not want to do it for two reasons. 1. They may not have same common parent exception. 2. The re-throw class may not a checked exception declared on the interface.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.