In 2011 I spoke about Ruby exception-handling at a number of conferences. One of those conferences was Rocky Mountain Ruby in Boulder, Colorado.
This talk was unique for a couple of reasons. First, I stripped out all the design and philosophy elements. Instead, I made it a code-heavy tour of little-known and seldom-seen features of the Ruby exception system. This let me relax a bit more and have more fun with it.
The other unique aspect of this talk was that I included a crazy exception hack that I hadn’t even included in my book on the topic. Up till now I think conference attendees and viewers of the Confreaks video are the only people who have seen it.
Here’s the video:
And here, for the first time outside of my slides, is the code for ignorable exceptions.
require 'continuation' # Ruby 1.9
class Exception
attr_accessor :continuation
def ignore
continuation.call
end
end
module RaiseWithIgnore
def raise(*args)
callcc do |continuation|
begin
super
rescue Exception => e
e.continuation = continuation
super(e)
end
end
end
end
class Object
include RaiseWithIgnore
end
def whiny_method
puts "Before raising"
raise "Pay attention to me!"
puts "After raising"
end
begin
whiny_method
rescue => e
puts "Ignoring '#{e}'"
e.ignore
end
# >> Before raising
# >> Ignoring 'Pay attention to me!'
# >> After raising
I do not endorse this code for production use, but I think if nothing else it’s an interesting look at the sort of lunacy and/or awesomeness that is possible with continuations.
Fantastic! By the way, I should note that Scheme, which pioneered continuations, ran into problems when dynamic-wind was introduced into the language: http://okmij.org/ftp/continuations/against-callcc.html#dynamic_wind
The interactions of all these kinds of forms of control gets very complicated fast.