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?

Marko
  • 30,263
  • 18
  • 74
  • 108
João
  • 2,296
  • 5
  • 20
  • 30

8 Answers8

5

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

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

Darron
  • 21,309
  • 5
  • 49
  • 53
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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
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
}
Marko
  • 30,263
  • 18
  • 74
  • 108
0

I suggest using the "Execute Around" idiom.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

Try this:

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

(pseudo code)

defnull
  • 4,169
  • 3
  • 24
  • 23
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)

luiscubal
  • 24,773
  • 9
  • 57
  • 83
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();
}
Dennis C
  • 24,511
  • 12
  • 71
  • 99
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;
}
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 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. – Dennis C Dec 04 '08 at 14:53