summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-catch/mrblib/catch.rb
blob: 68b165c8d5bea3372677b2fddf9407bbb3dc92cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ThrowCatchJump < Exception
  attr_reader :_tag, :_val
  def initialize(tag, val)
    @_tag = tag
    @_val = val
    super("uncaught throw #{tag.inspect}")
  end
end

module Kernel
  def catch(tag=Object.new, &block)
    block.call(tag)
  rescue ThrowCatchJump => e
    unless e._tag.equal?(tag)
      raise e
    end
    return e._val
  end
  def throw(tag, val=nil)
    raise ThrowCatchJump.new(tag, val)
  end
end