Created
March 7, 2011 02:55
-
-
Save jimweirich/857993 to your computer and use it in GitHub Desktop.
How to organize exceptions in your library
This file contains 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
# 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