From: Charles Comstock Date: 2004-04-06T04:29:20+09:00 Subject: Re: deciding between ruby and python Vadim Nasardinov wrote: > On Monday 05 April 2004 13:59, Charles Comstock wrote: > >>there is no comparable operation in java, without explictely >>declaring a wrapping try / catch / finally. > > > $ cat Main.java > public class Main { > public static void main(String[] s) { > new WithCriticalResource() { > public void action() { > System.out.print("code executed"); > } > }.run(); > > > new WithCriticalResource() { > public void action() { > throw new RuntimeException("let someone else mop this up"); > } > }.run(); > } > } > > abstract class WithCriticalResource { > > public final void run() { > try { > // set up critical resource > System.out.print("resource opened, "); > action(); > } finally { > System.out.println(", resource closed."); > } > } > > protected abstract void action(); > } > > $ java -cp . Main > resource opened, code executed, resource closed. > resource opened, , resource closed. > Exception in thread "main" java.lang.RuntimeException: let someone else mop this up > at Main$2.action(Main.java:12) > at WithCriticalResource.run(Main.java:25) > at Main.main(Main.java:10) > > > > > You can do the same thing in C# 1.0. > > > > Yea I knew that was a possibility, but that seperates logic from point of use in a way that is nice sometimes, but especially for alot of short things can be kind of distracting. It's the anonymous blocks combined with that syntax that wind up being so nice. Perhaps you can trick python into using a lambda with construct like this to make sure the logic stays inline as oppposed to jumping somewhere else, but I have heard sketchy things about the python lambda. Charles Comstock