On Exceptions

Since I just upgraded WordPress (It’s like it dreams of being as secure as bind or sendmail), I could write another article on why wordpress sucks, but that is boring and everyone has so instead I’ll write something about exception handling.

Code based on examples from sun javadocs on exceptions

try {
  doStuff();
} catch (ArrayIndexOutOfBoundsException e) {
  // Die Violently
} catch (IOException e) {
  // Die Violently
} catch (PuppyOnFireException e) {
  // Gasp in horror
} catch (MilkLeftOutOvernightException e) {
  // Become Annoyed
} catch (SuperNovaException e) {
  // Die Violently
} catch (BirdFluException e) {
  // Die Violently
} finally {
  // Oh make it stop make it stop!
}

Eww… I’m not impressed. There are some ideas floating around, and I’m sure I’m not the only one who has come up with this, but it seems like we already have a compressed syntax for these things. I present Case’d Exceptions:

try {
  doStuff();
} catch (e) {
  switch ArrayIndexOutOfBoundsException:
  switch IOException:
  switch SuperNovaException:
    // Die Violently
    break;
  switch PuppyOnFireException:
    // Gasp in horror
    break;
  switch MilkLeftOutOvernightException:
    // Become Annoyed
    break;
} finally {
    // Oh make it stop make it stop!
}

Ok now everyone argue about Fall Through and how horrible it is. And the syntax could probably be cleaned up.

It’s interesting, Python kind of irritates me with it’s “whitespace as syntax” (I was tempted to write “nonsense”), but I think it’s just because I’m getting old and grumpy. I actually tend to favor syntax with fewer {}’s all over the place. They’re not magical. In fact they’re kind of ugly and noisy and gross. There’s not a significant line savings with Case’d Exceptions, but to me it does look marginally cleaner.

And after all, cleanliness is next to godliness… and kittens… or something.

Coming soon: a blog entry on how the JVM handles polymorphism. Why? Because I got my ass thoroughly handed to me on an interview question on exactly that topic. Ow.

PS: I still frown on the following, I’m just lazy, and in this case it does make things a little less ridiculous:

try {
  // I hate when brackets are on the same line as syntax/code
}

Comments (2) left to “On Exceptions”

  1. shey wrote:

    Looking at your idea, I find it kind of verbose, it could get gross and I have a preference for:

    catch(DonkeyException d, MonkeyException m, KittyException k)
    {

    }

    assuming java wouldn’t complain that I didn’t use variables d,m or k.

  2. James Thigpen wrote:

    But that implies multiple exceptions being thrown, which can’t(?) occur. It’d be more like DonkeyException, MonkeyException, KittyException E, but then you lose exactly which type was thrown.

Post a Comment

*Required
*Required (Never published)