diff --git a/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java b/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java index 5248ac808..1eb19529c 100644 --- a/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java +++ b/core/src/main/java/org/teavm/ast/decompilation/Decompiler.java @@ -415,7 +415,7 @@ public class Decompiler { } } - for (int j = oldBlock.tryCatches.size() - 1; j >= 0; --j) { + for (int j = 0; j < oldBlock.tryCatches.size(); ++j) { TryCatchBookmark bookmark = oldBlock.tryCatches.get(j); TryCatchStatement tryCatchStmt = new TryCatchStatement(); tryCatchStmt.setExceptionType(bookmark.exceptionType); @@ -428,7 +428,7 @@ public class Decompiler { if (!tryCatchStmt.getProtectedBody().isEmpty()) { blockPart.add(tryCatchStmt); } - inheritedBookmarks.add(bookmark); + inheritedBookmarks.add(0, bookmark); } oldBlock.tryCatches.clear(); } diff --git a/tests/src/test/java/org/teavm/vm/VMTest.java b/tests/src/test/java/org/teavm/vm/VMTest.java index 271188f2a..72bbe6f4d 100644 --- a/tests/src/test/java/org/teavm/vm/VMTest.java +++ b/tests/src/test/java/org/teavm/vm/VMTest.java @@ -101,6 +101,24 @@ public class VMTest { assertEquals("before;inside;after;finally;", sb.toString()); } + @Test + public void catchFinally() { + StringBuilder sb = new StringBuilder(); + try { + if (Integer.parseInt("invalid") > 0) { + sb.append("err1;"); + } else { + sb.append("err2;"); + } + sb.append("err3"); + } catch (NumberFormatException e) { + sb.append("catch;"); + } finally { + sb.append("finally;"); + } + assertEquals("catch;finally;", sb.toString()); + } + @Test public void surrogateInStringLiteralsWork() { assertEquals(0xDDC2, "a\uDDC2b".charAt(1));