Skip to content

Instantly share code, notes, and snippets.

@pabloh
Last active August 29, 2015 14:27
Show Gist options
  • Save pabloh/81a6e789a8f4ec0819f9 to your computer and use it in GitHub Desktop.
Save pabloh/81a6e789a8f4ec0819f9 to your computer and use it in GitHub Desktop.
Resume from exception
class UberException < Exception
attr_reader :source
def initialize(source, context)
@source, @context = source, context
end
def resume(val = nil)
@context.call val # Method context lost: LocalJumpError
end
end
module Kernel
private
def uber_raise(exception)
context = proc {|val| return val }
raise UberException.new(exception, context)
end
end
if __FILE__ == $0
def foo
bar
rescue UberException => ue
puts "error captured"
ue.resume
end
def bar
puts "into bar"
uber_raise ArgumentError.new
puts "after error"
end
foo
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment