From: Kurt Stephens Date: 2008-02-11T11:25:55+09:00 Subject: Re: Restartable Exceptions Brent Roman wrote: > I think a "restartable" Exception could be implemented by > creating an Exception with an instance variable that holds > a Continuation. The Continuation would assigned when > the Exception was created such that when .call'd it > resumed (or restarted) execution at the desired point. > > It would be the responibility of the code that initially > threw the restartable exception to set up the Continuation > properly. > If we changed raise: 1) take a block for the continuation, 2) return the value of the restartable exception's continuation call begin File.open_restartable(file, mode) do | fh | process_file fh end rescue RestartableFileOpenError => err $stderr.write "Problem opening file: #{err.inspect}: try another? " different_file = $stdin.readline if different_file err.call different_file # does not return. end raise err end def File.open_restartable file, mode, &blk result = nil do begin result = File.open(file) do | fh | yield blk end file = nil rescue Exception => err raise RestartableFileOpenError, :error => err, :file => file do | new_file | file = new_file end end while file result end Uncaught restartable errors at top-level could invoke a debugger. > I believe no new formalisms would be required to implement this. > However, Continuations are quite expensive. > Would you want *all* Exceptions to be resumable in this way? > No. Too bad continuations are so expensive in Ruby. Are they really going away in 2.0? http://kurtstephens.com