void () { String code = """ for (int i = 0; i < MIMEHelper.MIME_MAP.length; i++) { String s = aFileNameWithOutExtention + MIMEHelper.MIME_MAP[i][0][0]; try { if (ENABLE_Z107_READING) { in = aFileNameWithOutExtention.getClass().getResourceAsStream(s); } else { data = ResourceHelper.readResource(s); } mime = MIMEHelper.MIME_MAP[i][1][0]; break; } catch (final Exception e) { continue; } }"""; String toFind = "try { '_TryStatement*; } catch(Exception '_ExceptionDcl) { '_CatchStatement*; }"; String replacement = "try { $TryStatement$; }\n" + "catch(Throwable $ExceptionDcl$) { $CatchStatement$; }"; String expectedResult = """ for (int i = 0; i < MIMEHelper.MIME_MAP.length; i++) { String s = aFileNameWithOutExtention + MIMEHelper.MIME_MAP[i][0][0]; try { if (ENABLE_Z107_READING) { in = aFileNameWithOutExtention.getClass().getResourceAsStream(s); } else { data = ResourceHelper.readResource(s); } mime = MIMEHelper.MIME_MAP[i][1][0]; break; } catch(final Throwable e) { continue; } }"""; assertEquals("Replacing try/catch in loop", expectedResult, replace(code, toFind, replacement)); }