Module: Kernel

Defined in:
lib/cond/kernel_raise.rb

Instance Method Summary collapse

Instance Method Details

#raise(*args) ⇒ Object Also known as: fail



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cond/kernel_raise.rb', line 4

def raise(*args)
  if Cond.handlers_stack.last.empty?
    # not using Cond
    Cond.original_raise(*args)
  else
    last_exception, current_handler = Cond.exception_stack.last
    exception = (
      if last_exception and args.empty?
        last_exception
      else
        begin
          Cond.original_raise(*args)
        rescue Exception => e
          e
        end
      end
    )
    if current_handler
      # inside a handler
      handler = loop {
        Cond.reraise_count += 1
        handlers = Cond.handlers_stack[-1 - Cond.reraise_count]
        if handlers.nil?
          break nil
        end
        found = Cond.find_handler_from(handlers, exception.class)
        if found and found != current_handler
          break found
        end
      }
      if handler
        handler.call(exception)
      else
        Cond.reraise_count = 0
        Cond.original_raise(exception)
      end
    else
      # not inside a handler
      Cond.reraise_count = 0
      handler = Cond.find_handler(exception.class)
      if handler
        Cond.exception_stack.push([exception, handler])
        begin
          handler.call(exception)
        ensure
          Cond.exception_stack.pop
        end
      else
        Cond.original_raise(exception)
      end
    end
  end
end