Created
July 14, 2015 23:34
-
-
Save natematias/fa5e877738f571a7ae8b to your computer and use it in GitHub Desktop.
Multiple-attempt exception handling for class methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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