Created
April 29, 2011 14:12
-
-
Save iNecas/948354 to your computer and use it in GitHub Desktop.
Why it is not a good idea to rescue without explicit exception type?
This file contains hidden or 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
# Why it is not a good idea to rescue without explicit exception type? | |
# Because: | |
# | |
# begin | |
# "..." | |
# rescue | |
# end | |
# | |
# Handles only StandardError descendants (no Excpetion descendants catched) | |
class SampleException < Exception; end | |
class SampleError < StandardError; end | |
begin | |
raise SampleError.new("This is exception") | |
rescue Exception => e | |
puts e.message # we have got here | |
end | |
begin | |
raise SampleException.new("This is exception") | |
rescue Exception => e | |
puts e.message # we have got here | |
end | |
begin | |
raise SampleError.new("This is exception") | |
rescue => e | |
puts e.message # we have got here | |
end | |
begin | |
raise SampleException.new("This is exception") | |
rescue => e | |
puts e.message # WE HAVEN'T GOT HERE !!!!!!!!!! | |
end | |
begin | |
raise SampleException.new("This is exception") | |
rescue | |
puts "we got here" # WE HAVEN'T GOT HERE NEITHER !!!!!!!!!! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment