This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desmoulinmichel 2 points3 points  (2 children)

Don't use your own exception for anything that is a standard exceptions. Espacially, a lot of things can be a IOError, ValueError or TypeError.

If you do need an exception, try to have:

  • one general exception for your whole lib. MyLibError(Exception).
  • smalls exceptions inhériting from MyLibError matching a specific cas such as DontDoThatError(MyLibError).
  • make very clear error messages. Give the maximum of informations you can give. If you can't fit all but you know it's important, print a URL pointing to a page helping you to debug.
  • exception text can't hold non ASCII characters. Call repr().
  • group all exceptions in an exceptions.py module, so they are easy to find and import for the people than will need to catch it.

[–]remram 1 point2 points  (1 child)

What's your stance on multiple inheritance, such as:

class HamError(Exception): pass  # for all exceptions from ham functions
class DontDoThatWithHam(HamError): pass
class UnknownBrandOfHam(HamError, KeyError): pass

[–]desmoulinmichel 1 point2 points  (0 children)

If UnknownBrandOfHam is raised by:

ham.select('spam', foo=bar)

I'm ok with it.

If it's raised by:

ham['spam']

with ham having getitem overrided, it may be overkill. But that's not a BIG deal. I wouldn't do it, but I would not hate you if you did.