Try/Catch DU Exception

Attila Kelemen attila.kelemen85 at gmail.com
Sat Jul 13 21:53:39 UTC 2024


>
> OK let's try going down this route just for fun...  :)
>
> Here's a sketch of how the spec might accommodate this.
>
> For any type E extending Throwble and expression or statement X, first
> define whether "X might throw E" as follows:
>
>    - (Precise Special Exceptions) If E is assignable from one of:
>    ArithmeticException, ArrayIndexOutOfBoundsException, ArrayStoreException,
>    ClassCastException, AssertionError, ExceptionInInitializerError,
>    MatchException, NegativeArraySizeException, NullPointerException, (others?)
>    and X is one of those expressions which the JLS defines to possibly throw
>    that exception, then X might throw E.
>    - (Imprecise Special Exceptions) If E is assignable from one of:
>    LinkageError (or any of its defined subclasses), BootstrapMethodError,
>    StackOverflowError, InternalError, OutOfMemoryError, ThreadDeath (others?),
>    then X might throw E.
>    - (Checked Exceptions) If E extends neither Error nor
>    RuntimeException, and X is an invocation of a method that declares it
>    throws something assignable to E, then X might throw Ei.
>    - (Unchecked Exceptions) If E extends Error or RuntimeException, and X
>    is an invocation of a method, then X might throw E.
>
> Now consider a catch block that catches E₁ | E₂ | ...
>
> Then the new rule would be:
>
>    - For any variable V, then V is definitely [un]assigned at the start
>    of the catch block iff the following is true for every Eᵢ: for every X in
>    the try block that might throw Eᵢ, V is definitely [un]assigned before X.
>
> So most of the new complexity here seems to be in dealing with all the
> "special exception" cases.
>
>
That formalization looks very nice to me. Definitely better than the foggy
idea I had in my head. And what is most appealing to me is that it actually
cleanly completes the flow to the catch blocks including both DA and DU.
So, given that, even this would work (not very useful, but at least shows
completeness):

```
int x;
try {
  x = 5;
  f();
} catch (Exception e) {
  System.out.println(x);
}
```

I guess the only thing missing from the definition is the finally block for
completeness, and that just have to include the "exit points" of the try
block (return, continue, break, and post last statement) in your "for every
X" (and otherwise work like `catch(Throwable)`).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240713/bd0dfc13/attachment-0001.htm>


More information about the amber-dev mailing list