Created
June 4, 2012 07:02
-
-
Save mpouleijn/2866841 to your computer and use it in GitHub Desktop.
5 things you didn't know about exceptions - By Avdi Grimm
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
# What the heck is $! ? | |
require 'English' | |
puts $!.inspect | |
begin | |
raise "Oops" | |
rescue | |
puts $!.inspect | |
puts $ERROR_INFO.inspect | |
end | |
puts $!.inspect | |
# nil | |
# #<RuntimeError: Oops> | |
# #<RuntimeError: Oops> | |
# nil |
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
Here is the video | |
http://www.youtube.com/watch?v=p5BdCR-fcTI&feature=youtu.be#t=17m48s |
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
# case | |
case obj | |
when Numeric, String, NilClass, FalseClass, TrueClass | |
puts "scalar" | |
# ... | |
end | |
# rescue | |
rescue SystemCallError, IOError, SignalException | |
# handle exception... | |
end |
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
# Add a simple crash logger | |
at_exit do | |
if $! | |
open['crash.log', 'a'] do |log| | |
error = { | |
:timestamp => Time.now, | |
:message => $!.message, | |
:backtrace => $!.backtrace, | |
:gems => Gem.loaded_specs.inject({}){ | |
|m, (n, s) | m.merge(n => s.version) | |
} | |
} | |
YAML.dump(error, log) | |
end | |
end | |
end | |
# https://github.com/avdi/zero-zero |
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
# Nested exceptions | |
class MyError < StandardError | |
attr_reader :original | |
def initialize(msg, original=$!) | |
super(msg); | |
@original = original | |
end | |
end | |
begin | |
begin | |
raise "Error A" | |
rescue => error | |
raise MyError, "Error B" | |
end | |
rescue => error | |
puts "rescued: #{error.inspect}" | |
puts "Original: #{error.original.inspect}" | |
end |
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 Net::HTTPResponse | |
def exception(message="HTTP Error" | |
RuntimeError.new("#{message}: #{code}") | |
end | |
end | |
#... | |
responde = Net::HTTP.get_response(url) | |
raise response |
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
module RaiseExit | |
def raise(msg_or_exc, msg = msg_or_exc, trace = caller) | |
warn msg.to_s | |
exit! 1 | |
end | |
end | |
class Object | |
include RaiseExit | |
end | |
# https://github.com/avdi/hammertime |
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
# Retry | |
tries = 0 | |
begin | |
tries =+ 1 | |
puts "Trying #{tries}..." | |
raise "Didn't work" | |
rescue | |
retry if tries < | |
puts "I give up" | |
end | |
# Trying 1... | |
# Trying 2... | |
# Trying 3... | |
# I give up |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment