Feature Request for the JLS: Auto-Rethrow

Java 7 has eased some pain in the area of exception handling when the new try-with-resources and multi-catch syntaxes were introduced. The latter is very interesting from a syntax perspective because it is the only place in Java where formal union types are allowed, similar to what Ceylon offers. Remember, a union type A | B is a type has all the properties that both types share. For example:

try {
    // ...
}
catch (SQLDataException | SQLWarning e) {

    // Properties that both types share:
    System.out.println(e.getSQLState());
}

The above can be quite useful in cases when we simply want to re-throw a variety of similar but disjoint exceptions in the same way. But often, we want do this:

// Some code
try {
}

// Rethrow
catch (OurException e) {
    throw e;
}

// Wrap other types of exceptions in OurException
catch (Exception e) {
    throw new OurException(e);
}

That’s quite a few lines of code for something that we do all the time and that could be called “rethrow-or-wrap” pattern. How about if we could rewrite the above as such:

// Some code
try {
}

// Re-throw exceptions of type OurException
// ... and wrap all other types in OurException
throws OurException;

Without a catch clause, this would be exactly the same as our previous example. Rethrow or wrap. Java is a language that heavily recycles keywords, so a dedicated keyword is out of question. throws would be a good keyword here as it is somewhat similar to the throws clause of a method signature, i.e. the throws clause of the try block. If we only want to rethrow some exceptions and write tailor-made catch blocks for other types of exceptions, we could still do the same:

// Some code
try {
}

// Re-throw exceptions of type OurException
throws OurException

// But deal with all other types here
catch (Exception e) {
    throw new OurException("Custom message", e);
}

Java should have a “rethrow Exception” statement. tweet this
What are your thoughts on this? Would such a syntax pull its own weight?

One thought on “Feature Request for the JLS: Auto-Rethrow

  1. While I think so form of rethrow syntax might be a good idea, I take exception (haha!) with your examples. catch(Exception e) is rarely a good idea. And usually when I see it in code it is a sign of the developer being lazy…”oh this thing can have exceptions, I don’t want to deal right now, lets just catch whatever and do something to make it go away”. While this is not necessarily the case intended by your example, it is a pattern that encourages sloppiness. There are several problems with catching Exception:

    1) It catches RuntimeExceptions too.
    2) It may catch exception A and B that you know the method you are using will throw, and have thought about, and wish to handle this way. After updating the library with this function (or after code changes by a coworker), suppose this method now thorws exception C also? If you are catching Exception, you will just automatically start handing that exception the same way as A and B without noticing or making any decision about this.

    I would also note that the only reason you want this bit:

    catch (OurException e) {
        throw e;
    }
    

    is because of this sloppy catch Exception which defeats the normal way of doing that, which is simply by having a throws clause on your function.

Leave a Reply