Skip to content

Instantly share code, notes, and snippets.

@iNecas
Created April 29, 2011 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iNecas/948354 to your computer and use it in GitHub Desktop.
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?
# 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