JavaPosse 135 - Catch more than one Exception

1 view
Skip to first unread message

Bernd Schiffer

unread,
Aug 8, 2007, 2:49:49 PM8/8/07
to The Java Posse
Hi.

You talked about catching more than one exception at once using Java
Generics. Is there an example out there where I can see how exactly
that would be possible?

Thanks in advance,
Bernd

Rick

unread,
Aug 8, 2007, 7:38:17 PM8/8/07
to The Java Posse
I believe they were talking about a hypothetical proposal by someone.
I don't think there is a working version, as this probably requires
some major changes down at the compiler level.

On Aug 9, 4:49 am, Bernd Schiffer <bernd.schif...@googlemail.com>
wrote:

Bernd Schiffer

unread,
Aug 9, 2007, 5:16:44 AM8/9/07
to The Java Posse
Oh, I see - it's a pity :-) Would be nice if you'd write something
like:

try {
statementWithTwoCheckedExceptions();
} catch(Throwable<CheckedException1, CheckedException2> throwable) {
doSomethingWith(throwable);

Jess Holle

unread,
Aug 9, 2007, 7:31:24 AM8/9/07
to java...@googlegroups.com
Bernd Schiffer wrote:
Oh, I see - it's a pity :-) Would be nice if you'd write something
like:

try {
   statementWithTwoCheckedExceptions();
} catch(Throwable<CheckedException1, CheckedException2> throwable) {
   doSomethingWith(throwable);
}
  
As was pointed out on the podcast using the <> from generics would cause the reader to expect to be able to use this comma-delimited list in many other places that make no sense at all.

I do agree that something of this sort would be nice but the <> syntax would cause no end of grief.

Perhaps something like:
catch ( CheckedException1, CheckedException2 e )
{
  ...
}
where the compiler was responsible for figuring out the most derived Throwable subclass shared by the exceptions listed.

--
Jess Holle

Mark Derricutt

unread,
Aug 9, 2007, 7:54:38 AM8/9/07
to java...@googlegroups.com
Why not do a variation on the array syntax:

  catch (new Throwabe[] {CheckedException1, CheckedException2}) {
     ...
  }

If the language spec was then to take advantage of JDK5's optional parameters you could even drop the declaration back to

  catch (CheckedException1, CheckedException2) {
    ...
  }

Mark

On 8/9/07, Jess Holle <je...@ptc.com > wrote:

Jess Holle

unread,
Aug 9, 2007, 8:14:17 AM8/9/07
to java...@googlegroups.com
Mark Derricutt wrote:
Why not do a variation on the array syntax:

  catch (new Throwabe[] {CheckedException1, CheckedException2}) {
     ...
  }

If the language spec was then to take advantage of JDK5's optional parameters you could even drop the declaration back to

  catch (CheckedException1, CheckedException2) {
    ...
  }
That implies all the code within the catch gets an array of exceptions, which is not what you want -- nor what you want to imply.

You just want to simpler syntax for:
catch (ExceptionType1 e)
{
  handleException( e );
}
catch (ExceptionType2 e )
{
  handleException( e );
}
catch (ExceptionType3 e )
{
  handleException( e );
}
...

void  handleException( ExceptionBaseClass1 e )
  throws SomeExceptionType
{
  ...
}
Of course that's not the worst thing to have to write -- it'd just be nicer not to have to go this far...

--
Jess Holle

Oigres

unread,
Aug 9, 2007, 9:51:10 AM8/9/07
to The Java Posse
Another reason that a generified approach cannot be made to work is
that a catch block is basically an instanceof test. You can only
apply that to a type that is reifable. As all paramterised type
information is erased at runtime, the vm cannot distinguish between
the paramertised Throwables.

Interesting idea, but to me this whole thing smells like trying to
solve a problem that doesn't really exist.

Sergio

Kevin Wong

unread,
Aug 9, 2007, 10:58:40 AM8/9/07
to The Java Posse
I proposed this a while ago in another thread:

http://groups.google.com/group/javaposse/browse_thread/thread/231a84695140a38e/37510c18bd6b85a1?lnk=gst&q=catch&rnum=1#37510c18bd6b85a1

I couldn't find the link to the generics proposal the Posse mentioned
in the podcast.

Reply all
Reply to author
Forward
0 new messages