Module: TryCatch::Function

Defined in:
lib/try-catch.rb

Class Method Summary collapse

Class Method Details

.catch(self_object, *exception_classes, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/try-catch.rb', line 65

def catch( self_object, *exception_classes, &block )

  exception_classes.each do |exception_class|
    unless exception_class <= Exception
      raise ArgumentError, "invalid Exception: #{exception_class}"
    end
  end

  exception_classes.push(Exception) if exception_classes.empty?

  if self_object.class <= Exception
    exception_obj= self_object
  else
    exception_obj= TryCatch::Memory.load( self_object.object_id )
  end

  return nil if exception_obj.nil?
  return exception_obj if block.nil?



  if exception_classes.map{ |exc| exception_obj.class <= exc }.include?(true)

    if block.parameters.empty?
      return block.call
    else
      return block.call exception_obj
    end

  else
    return exception_obj
  end

end

.try(self_object, *exception_classes, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/try-catch.rb', line 46

def try( self_object, *exception_classes, &block )

  exception_classes.each do |exception_class|
    unless exception_class <= Exception
      raise ArgumentError, "invalid Exception: #{exception_class}"
    end
  end

  exception_classes.push(Exception) if exception_classes.empty?

  begin
    return block.call
  rescue *exception_classes => ex
    TryCatch::Memory.save( self_object.object_id, ex )
    return ex
  end

end