Skip to content

fix(java): wrap bare rethrows of checked exceptions - #62

Merged
kroitor merged 1 commit into
masterfrom
fix/java-bare-rethrow-checked-exception
Jul 31, 2026
Merged

fix(java): wrap bare rethrows of checked exceptions#62
kroitor merged 1 commit into
masterfrom
fix/java-bare-rethrow-checked-exception

Conversation

@kroitor

@kroitor kroitor commented Jul 31, 2026

Copy link
Copy Markdown
Member

What broke

printThrowStatement's Identifier branch emits a bare rethrow (throw e) verbatim. In the transpiled ccxt Java, method bodies live inside CompletableFuture lambdas which cannot declare throws, so rethrowing the caught (checked) java.lang.Exception fails compilation:

error: unreported exception Exception; must be caught or declared to be thrown
    try { ... } catch (Exception e) { throw e; }
                                      ^

First real-world trigger: the mexc ws authenticate fix (ccxt/ccxt#29392) — the first bare rethrow ever transpiled to Java in ccxt — whose auto-committed emission broke ccxt's java.yml Build Project step (failing run).

Fix

Emit a conditional wrap in the Identifier branch:

throw (e instanceof RuntimeException ? (RuntimeException)e : new RuntimeException(e));
  • unchecked exceptions — including all ccxt error types, which extend RuntimeException — rethrow with identity intact, so upstream catch dispatch on ccxt exception types is unaffected
  • genuinely checked exceptions get wrapped with the original as the cause
  • other language emitters untouched (their targets accept bare rethrow)

Verified against ccxt

  • patched emitter → regenerated java/lib/.../pro/MexcCore.java → emits the conditional form at the previously-broken site
  • isolated javac proof: the wrapped construct compiles inside a supplyAsync lambda; the previous emission reproduces the exact CI error

`throw e` inside a `catch (Exception e)` block was emitted verbatim, but
java.lang.Exception is checked and the CompletableFuture lambdas that the
transpiled ccxt method bodies live in cannot declare `throws` - javac
fails with "unreported exception Exception". First triggered by the mexc
ws authenticate fix (ccxt/ccxt#29392), whose java emission broke the ccxt
java build. The identifier branch now emits a conditional wrap:

    throw (e instanceof RuntimeException ? (RuntimeException)e : new RuntimeException(e));

which keeps unchecked exceptions (all ccxt errors extend RuntimeException)
rethrown with their identity intact and wraps genuinely checked ones with
the original as the cause. Verified against ccxt: regenerated MexcCore.java
compiles the construct; the previous emission reproduces the javac error.
@kroitor
kroitor merged commit 77d2339 into master Jul 31, 2026
1 check passed
@kroitor kroitor self-assigned this Jul 31, 2026
@kroitor kroitor added the bug Something isn't working label Jul 31, 2026
kroitor added a commit to ccxt/ccxt that referenced this pull request Jul 31, 2026
Pins ast-transpiler to current master (35154c4, parent: the #62 squash),
picking up ccxt/ast-transpiler#62: bare rethrows of checked exceptions
now emit as a conditional RuntimeException wrap, fixing the java.yml
Build Project failure introduced when the mexc ws authenticate fix
(#29392) became the first `throw e` transpiled to Java. ccxt error types
extend RuntimeException and keep their identity through the wrap.
Verified locally with the fix-equivalent dist: MexcCore.java emits the
compilable form, isolated javac proof both ways, C#/PHP/Python legs clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant