// Syntax: void method() no exceptions { doSomething(); } // (rethrows all inner exceptions as runtime exceptions) // // Alternative syntax: void method() ctex { doSomething(); } // (ctex for catches thrown exceptions or something like that) !592 1000265 // auto-importer, standard function adder public class main { public static void main(String[] args) throws Exception { String s = loadMainJava(); Pattern regex = Pattern.compile("\\s+(no\\s+exceptions|ctex|null on exception)\\s*\\{"); for (int i = 0; i < 100; i++) { Matcher matcher = regex.matcher(s); if (!matcher.find()) break; String kind = matcher.group(1); System.out.println("Iteration " + (i+1)); int start = matcher.start(), end = matcher.end(); int endOfBlock = findEndOfBlock(s, end); String catchBlock, catchWhat; if (kind.startsWith("null")) { catchBlock = "return null;"; catchWhat = "Throwable"; } else { catchBlock = "throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e);"; catchWhat = "Throwable"; } String tryBlock = " { try {\n " + s.substring(end, endOfBlock) + "\n} catch (" + catchWhat + " __e) { " + catchBlock + " }"; s = s.substring(0, start) + tryBlock + s.substring(endOfBlock); } saveMainJava(s); } // start is the index AFTER the opening bracket // returns index OF closing bracket static int findEndOfBlock(String s, int start) { int level = 1; for (int i = start; i < s.length(); i++) { if (s.charAt(i) == '{') ++level; else if (s.charAt(i) == '}') --level; if (level == 0) return i; } return s.length(); } }