module TryCatch::Function

Public Class Methods

catch( self_object, *exception_classes, &block ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source

try that return nil overall exception captured block fail to run

# 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 ) click to toggle source

no cache version !

# 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