Any chance to see Exception#resume in Ruby2 ?

G

gabriele renzi

Hi gurus and nubys,

I was looking at the cvs changelog and my eye fell over
the ToDo file (btw, it seem it is not been updated in the last ten
months, but is still interesting :)

In the file there is an interesting reference to resumable exceptions.

Now, I googled for them and found this:
http://www.rubygarden.org/article.php?sid=340

A rejected rcr about it. so I wonder what the final word is about
this.
 
D

Dan Doel

As I recall, this was discussed here before and it was decided that it was a
bad idea.

The argument went something like this:

When you throw an exception, you do so to indicate some sort of error.
Generally, this means that execution after that point can't continue, so
#resume doesn't make sense. The most you can do is retry, which starts
back at the beginning of the sequence of events to try the whole operation
over again.

I think the argument for it was that you could encounter a non-fatal error
condition, throw an exception to skip out, log the error, and skip back in
to continue down some alternate path. However, this isn't always possible,
and you have to specifically code for it.

So you'd have libraries that support resumable exceptions, and libraries
that don't, and trying to resume in one that doesn't causes big problems.
I think the general consensus was that Exceptions are supposed to
represent unrecoverable error conditions, so you couldn't resume. I
suppose you could implement some sort of recoverable exception using
continuations, but it might be ugly (actually, I'll think about it and see
what I can come up with).

Basically, Matz said it wouldn't be put in.

- Dan
 
Y

Yukihiro Matsumoto

Hi,

In message "Any chance to see Exception#resume in Ruby2 ?"

|In the file there is an interesting reference to resumable exceptions.
|
|Now, I googled for them and found this:
|http://www.rubygarden.org/article.php?sid=340
|
|A rejected rcr about it. so I wonder what the final word is about
|this.

I doubt it would be in 2.0, by following reason:

* performance: resumable exceptions require "raise" to keep its
continuation inside of exception, that might slow down the
performance.

* break assumption: although resumable exception is interesting
idea, I guess so many code assume "raise" not to resume.

* can be done explicitly: we can resume to the throw point of the
exception by using "retry" or "callcc" explicitly. we don't need
to generalize resuming exceptions. NOTE: I'm NOT the believer of
the "explicit better than implicit" principle though.

matz.
 
D

Dan Doel

This probably isn't the safest way to do it, but it works. Note that if your
arguments to #raise are wrong, and it throws an ArgumentError, that
will get wrapped with a #resume method. It's probably safer try and
handle all the possible argument combinations to #raise properly, but
this way is a lot easier for a proof-of-concept.

Enjoy.

- Dan

[dolio 11:35:18 ~] $ cat prac.rb
#!/usr/bin/ruby

def recoverable_raise(*args)
raise *args
rescue Exception => e
callcc do |cc|
scls = class << e; self; end
scls.send:)define_method, :resume, lambda { cc.call })
raise
end
end

begin
puts "foo"
recoverable_raise "baz"
puts "bar"
rescue Exception => e
p e
e.resume
end
[dolio 11:35:39 ~] $ ./prac.rb
foo
#<RuntimeError: baz>
bar
[dolio 11:36:00 ~] $
 
J

Jamis Buck

Dan said:
This probably isn't the safest way to do it, but it works. Note that if your
arguments to #raise are wrong, and it throws an ArgumentError, that
will get wrapped with a #resume method. It's probably safer try and
handle all the possible argument combinations to #raise properly, but
this way is a lot easier for a proof-of-concept.

Beautiful, Dan. And thus is the power of Ruby demonstrated once again.
Someone wants "raise" to be resumable, and it is eventually shown to be
possible using existing Ruby features.

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

ruby -h | ruby -e
'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a <<
r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'
 
G

gabriele renzi

As I recall, this was discussed here before and it was decided that it was a
bad idea.
<snip>

thanks for the answers, both dan and matz.
I still believe resume is cool, but now I see the dark side of it :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top