From: Robert Klemme Date: 2003-03-21T02:58:15+09:00 Subject: Re: class level Exception handling "Mauricio Fern�ndez" schrieb im Newsbeitrag news:20030320164056.GA11459@student.ei.uni-stuttgart.de... > On Fri, Mar 21, 2003 at 01:08:30AM +0900, Xiangrong Fang wrote: > > Hi > > > > Could anyone teach me about the following? > > > > 1. in a File.open do ... end block, if I call break or return to exit > > the block, will the file be closed? > > I guess yes, as it uses rb_ensure. Right. > module WrapMethod > def __block__(meth) > @blocks[meth] > end > def wrap(*args, &block) > @blocks ||= {} > args.each do |meth| > alias_method "__#{meth.to_s}__".intern, meth > @blocks[meth] = block > module_eval <<-EOF > def #{meth.to_s}(*args,&b) > begin > return __#{meth.to_s}__(*args,&b) > rescue Exception > return self.class.__block__(:#{meth.to_s}).call > end > end > EOF > end > end > end Did I miss something or do you store the same block in @block for each method? If so, why? The original poster wanted to have a single error handler for all methods. Another suggestion I would make is to change the rescue clause to: rescue Exception => e return self.class.__block__(:#{meth.to_s}).call(e) end and then wrap :foo, :bar, :baz do |ex| puts "Got #{ex.inspect}" false end This is more explicit and provides for easier access to the exception, because you need not remember that the exception is stored in $!. Regards robert