StringBuilder buf = new StringBuilder("try {\n");
buf.append(block.generate(context)).append("\n} ");
if (!catchBlocks.isEmpty()) {
for (Variable exception : catchBlocks.keySet()) {
Context ctx = Context.create(context).addVariable(exception);
buf.append("catch (").append(exception.generate(ctx)).append(") ")
.append("{\n")
.append(catchBlocks.get(exception).generate(ctx))
.append("\n} ");
}
}
else if (finallyBlock == null) {
finallyBlock = new BlockStatement();
}
if (finallyBlock != null) {
Context ctx = Context.create(context);
buf.append(" finally {\n").append(finallyBlock.generate(ctx)).append("\n}\n");
}
return generatedCache = buf.toString();
}