Skip to content

Instantly share code, notes, and snippets.

@benlangfeld
Created January 10, 2014 17:45
Show Gist options
  • Save benlangfeld/8359019 to your computer and use it in GitHub Desktop.
Save benlangfeld/8359019 to your computer and use it in GitHub Desktop.
How not to implement nested exceptions in Ruby
class FooException < StandardError
end
class BarException < StandardError
attr_reader :exception
def initialize(exception)
@exception = exception
end
end
begin
begin
raise FooException
rescue => e
puts "Got an inner #{e.class}...raising BarException with it nested."
raise BarException.new(e)
end
rescue => e
puts "Got an outer #{e.class}. This should only ever be a BarException, which wraps all other inner exceptions."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment