Skip to content

Instantly share code, notes, and snippets.

@shadoi
Created November 7, 2011 22:02
Show Gist options
  • Save shadoi/1346342 to your computer and use it in GitHub Desktop.
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
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