Module: TryCatch::Function
- Defined in:
- lib/try-catch/functions.rb
Class Method Summary collapse
- .catch(self_object, *exception_classes, &block) ⇒ Object
- .try(self_object, *exception_classes, &block) ⇒ Object
-
.try?(*exception_classes, &block) ⇒ Boolean
try that return nil overall exception captured block fail to run.
-
.try_no_cache(*exception_classes, &block) ⇒ Object
no cache version !.
Class Method Details
.catch(self_object, *exception_classes, &block) ⇒ Object
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 |
# File 'lib/try-catch/functions.rb', line 66 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 |
.try?(*exception_classes, &block) ⇒ Boolean
try that return nil overall exception captured block fail to run
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/try-catch/functions.rb', line 48 def try?( *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 return nil end end |
.try_no_cache(*exception_classes, &block) ⇒ Object
no cache version !
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/try-catch/functions.rb', line 29 def try_no_cache( *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 return ex end end |