The Artima Developer Community
Sponsored Link

Java Buzz Forum
Better exception syntax

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Norman Richards

Posts: 396
Nickname: orb
Registered: Jun, 2003

Norman Richards is co-author of XDoclet in Action
Better exception syntax Posted: Jul 14, 2007 12:40 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Norman Richards.
Original Post: Better exception syntax
Feed Title: Orb [norman richards]
Feed URL: http://members.capmac.org/~orb/blog.cgi/tech/java?flav=rss
Feed Description: Monkey number 312,978,199
Latest Java Buzz Posts
Latest Java Buzz Posts by Norman Richards
Latest Posts From Orb [norman richards]

Advertisement

I'm not an exception hater, but I do dislike the way Java makes you deal with multiple exception types. Take something trivial like reflection:

try {
    Field field = someClass.getDeclaredField(someField);
    someValue = field.getInt(Barcode.class);
} catch (SecurityException e) {
    // do something with the exception
} catch (NoSuchFieldException e) {
    // do something else with the exception
}

Now, the truth is that something and something else are almost always the same thing. I'm probably not going to do something different with SecurityException than I will with NoSuchFieldException. I'm going to log/wrap/deal with the exceptions in a generic way. I hate writing duplicate code so much, I'm much more likely to simply write this:

try {
    Field field = someClass.getDeclaredField(someField);
    someValue = field.getInt(Barcode.class);
} catch (Exception e) {
    // do something with the exception
}

However, that is definitely not the same behavior. That code catches all exceptions. In general, I would prefer that unchecked exceptions be passed on unmolested. However, I don't want to have write multiple catch blocks if I don't have to. What I'd really like something like this:

try {
    Field field = someClass.getDeclaredField(someField);
    someValue = field.getInt(Barcode.class);
} catch (SecurityException, NoSuchFieldException as Exception e) {
    // do something else with the exception
}

In my mind, that's the neatest way to uniformly deal with multiple exceptions. If you need to do something different with SecurityException than NoSuchFieldException, you can catch them separately.

The Java compiler wouldn't need to do anything special to support this. As far as I know, two entries in the exception table for a method can point to the same exception handler code. So, it's simply a matter of syntax.

Read: Better exception syntax

Topic: Oracle Real-Time Decisions, a short introduction Previous Topic   Next Topic Topic: Review: TrackStick II - GPS Tracker Integrated With Google Maps

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use