Skip to content

Instantly share code, notes, and snippets.

@MichaelDrogalis
Created January 22, 2013 17:34
Show Gist options
  • Save MichaelDrogalis/4596526 to your computer and use it in GitHub Desktop.
Save MichaelDrogalis/4596526 to your computer and use it in GitHub Desktop.
def divider a, b
a / b
end
handlers = {}
handlers[TypeError] =
Proc.new { |e| puts "Type exception occured." }
handlers[ZeroDivisionError] =
Proc.new { |e| puts "Division by 0 occured." }
def supervise p, args, handlers
begin
Thread.new {
p.call(args)
}.value
rescue => e
handlers[e.class].call(e)
end
end
proc = Proc.new {|a, b| divider(a, b)}
supervise(proc, [5, 5], handlers) # => 5
supervise(proc, [5, nil], handlers) # => Type exception occured.
supervise(proc, [5, 0], handlers) # => Division by 0 occured.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment