Created
June 3, 2011 02:25
-
-
Save banister/1005741 to your computer and use it in GitHub Desktop.
manveru's nifty restartable exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'continuation' | |
class RestartableError < StandardError | |
attr_accessor :cc | |
def self.call(msg) | |
instance = new(msg) | |
returning = true | |
callcc{|c| instance.cc = c } | |
returning = !returning | |
raise instance unless returning | |
end | |
end | |
class Foo | |
def a | |
puts "everything fine a" | |
RestartableError.call("omg!") | |
puts "everything fine b" | |
end | |
end | |
begin | |
Foo.new.a | |
rescue RestartableError => ex | |
p ex | |
p ex.cc.call | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment