Module: Kernel

Defined in:
lib/cond.rb

Instance Method Summary collapse

Instance Method Details

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



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/cond.rb', line 401

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