Skip to content

Support for multi-catch-clauses in try-statements#3919

Open
wadoon wants to merge 5 commits into
mainfrom
weigl/catchmulti
Open

Support for multi-catch-clauses in try-statements#3919
wadoon wants to merge 5 commits into
mainfrom
weigl/catchmulti

Conversation

@wadoon

@wadoon wadoon commented Jul 12, 2026

Copy link
Copy Markdown
Member

Intended Change

This PR implements a new transformer for the KeYJavaTransformationPipeline that desugars modern Java exception handling constructs into their equivalent basic forms:

MultiCatchReducer transforms multi-catch clauses into multiple single catch clauses (per JLS 14.20).

These transformations enable KeY to verify code using these Java 7+ features by reducing them to the core try/catch/finally constructs already supported by the verification infrastructure.

Technical Details

For a multi-catch:

try {
    body
} catch (ExceptionType1 | ExceptionType2 e) {
    handler
}

Produces:

try {
    body
} catch (ExceptionType1 e) {
    handler
} catch (ExceptionType2 e) {
    handler
}

Supports:

  • Any number of exception types in union
  • Mixed single and multi-catch in same try
  • Preserves finally-blocks
  • Marks exception parameters as final (as required by multi-catch semantics)

Type of pull request

  • Refactoring (behavior should not change or only minimally change)
  • There are changes to the Java code

Ensuring quality

  • I made sure that introduced/changed code is well documented (Javadoc and inline comments).
  • I added new test case(s) for new functionality.
    • MultiCatchReducerTest.java - Tests for 2-3 exception types, mixed catches, and finally blocks
  • I have tested the feature as an example: key.ui/examples/Java/MultiCatch/

@wadoon wadoon requested a review from unp1 July 12, 2026 12:52
@wadoon wadoon self-assigned this Jul 12, 2026
@wadoon wadoon force-pushed the weigl/catchmulti branch from e8fd54d to a8c2cbb Compare July 12, 2026 13:06

@unp1 unp1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.
Remaining issues:

  1. MultiCatchReducer is not added to the pipeline. It is defined under …/transformations/pipeline/ but not registered in KeYJavaPipeline.createDefault(...), so it never runs during loading

  2. Two or more multi-catch clauses in the same try are handled incorrectly. rewrite(...) iterates a snapshot (originalClauses) but mutates the live catchClauses via remove(i) / add(j++, …); after the first multi-catch expands, the snapshot index no longer matches the live list. As a result one clause is silently dropped and an un-expanded UnionType is left behind (which then fails to load). Reproducer:

    public class T {
        void m() {}
        int f() {
            try { m(); }
            catch (IllegalArgumentException | NullPointerException e) { return 1; }
            catch (ArithmeticException | ArrayStoreException e)       { return 2; }
            return 0;
        }
    }

    With the reducer registered this fails to load with Unsupported element … UnionType on the leftover ArithmeticException | ArrayStoreException, and the transformer output shows a clause from the first multi-catch is dropped. A single multi-catch (even mixed with single-type catches) works. Fix: track the live insertion position instead of removing by the snapshot index (e.g. rebuild the clause list, or remove the specific node).

  3. The bundled example does not load. key.ui/examples/Java/MultiCatch/MultiCatch.java uses java.sql.SQLException, which is not in KeY's default classpath (JavaRedux)

  4. Nit: the Javadoc note about ordering catch clauses by subtype relationship doesn't apply to a multi-catch's own alternatives, they must be disjoint (JLS §14.20), so there's no "more specific first" among them.

@wadoon wadoon marked this pull request as ready for review July 12, 2026 13:50
@wadoon wadoon added this to the v3.0.0 milestone Jul 12, 2026
@wadoon wadoon enabled auto-merge July 12, 2026 20:36
@wadoon wadoon requested a review from unp1 July 12, 2026 20:36
@wadoon

wadoon commented Jul 12, 2026

Copy link
Copy Markdown
Member Author

Thanks. Remaining issues:
addressed

@wadoon wadoon changed the title add MultiCatch transformation to KeY Support for multi-catch-clauses in try-statements Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants