Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created March 7, 2011 02:55
Show Gist options
  • Save jimweirich/857993 to your computer and use it in GitHub Desktop.
Save jimweirich/857993 to your computer and use it in GitHub Desktop.
How to organize exceptions in your library
# Suppose you were designing a library named "Zippo".
# How would you organize the exceptions thrown by the library.
# Option 1
# All library errors inheriting from a common base
module Zippo
class ZippoError < StandardError; end
class ZippoArgumentError < ZippoError; end
class ZippoTypeError < ZippoError; end
# ...
end
# Option 2
# Library errors inheriting from existing Ruby errors
# (at least where appropriate)
module Zippo
class ZippoArgumentError < ArgumentError; end
class ZippoTypeError < TypeError; end
end
# Option 3
#
# If you are going to throw and argument error, just throw the
# existing Ruby ArgumentError. If you have errors that don't map to
# existing errors, go with option 1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment