Created
January 10, 2014 17:45
-
-
Save benlangfeld/8359019 to your computer and use it in GitHub Desktop.
How not to implement nested exceptions in Ruby
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
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