Module: TryCatch::Function

Defined in:
lib/try-catch/functions.rb

Class Method Summary collapse

Class Method Details

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



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
57
58
59
# File 'lib/try-catch/functions.rb', line 28

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/try-catch/functions.rb', line 6

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 )
    TryCatch::Memory.memory_agent

    return ex

  end

end