Exception transparency - lone throws (no checked exceptions)

Stephen Colebourne scolebourne at joda.org
Thu Jun 10 07:08:08 PDT 2010


On 10 June 2010 14:30, Peter Levart <peter.levart at marand.si> wrote:
> Interesting proposal. You don't specify what happens if code in a method calls a method with "lone throws" but doesn't catch anything or catches only some exceptions (but not all Throwables). Does the calling method have to declare "lone throws" too?

I don't think that is practical - as you say, there would be no real
difference to throws Throwable that way.

So, here is how it would work:

  public void process() throws {
    throws new IOException();
  }

  public void worker() {
   process();   // can catch IOException here if we want
  }

  public void master() {
   worker();   // cannot catch IOException here
  }

The method worker() thus throws an IOException. Is this sneaky? Well,
the initial throw isn't - effectively, the initial throw is indicating
that anything may be thrown, and that the caller may catch anything.

However, once thrown, it just keeps on going (as per
RuntimeException). As such, the catch clause in master() won't compile
- worker() doesn't specify lone throws, so the old checked exception
rules apply.

With this scheme, there may be a case for an alteration to the catch
clause to allow it to be caught (which handles the increasingly common
sneaky throws in general):

  public void master() {
   try {
     worker();
   } catch (throws IOException ex) {}
  }

Overall, the scheme is not quite the same as throws Throwable, nor is
it the same as converting all checked exceptions to unchecked. Its
between the extremes.

Stephen


More information about the lambda-dev mailing list