Created
November 7, 2011 22:02
-
-
Save shadoi/1346342 to your computer and use it in GitHub Desktop.
Totally pointless file handling example to show how I'd use exceptions with retry and ensure
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 NotExecutable < RuntimeError; end | |
begin | |
f = File.open("test") | |
raise NotExecutable unless File.executable?(f) | |
rescue Errno::ENOENT => e | |
puts e.inspect | |
%x{touch test} | |
retry | |
rescue NotExecutable => e | |
%x{chmod +x test} | |
retry | |
rescue StandardError => e | |
puts "Unhandled Exception: #{e}" | |
puts e.backtrace | |
ensure | |
f.close rescue true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment