Skip to content

Instantly share code, notes, and snippets.

@natematias
Created July 14, 2015 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natematias/fa5e877738f571a7ae8b to your computer and use it in GitHub Desktop.
Save natematias/fa5e877738f571a7ae8b to your computer and use it in GitHub Desktop.
Multiple-attempt exception handling for class methods
class SillyError < StandardError; end
module CatchAnError
def self.included base
base.extend CatchSpecific
end
module CatchSpecific
def catch_specific_error(a,stdout)
num_attempts = 0
begin
num_attempts += 1
stdout.puts "attempt: #{num_attempts}"
yield
rescue SillyError
puts "Silly Error Raised"
return nil if (num_attempts >=2)
retry
end
end
end
end
class AwesomeModule
include CatchAnError
def self.run
self.catch_specific_error("hats",$stdout){
raise SillyError
}
end
end
AwesomeModule.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment