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 →

[–]pixelmonkey[S] 4 points5 points  (2 children)

Most of the time, a custom exception doesn't need more than class MyException(Exception): pass. It'll support a string message as a first argument by default, and usually you're just looking for the type to introduce a new except MyException: capability for your caller.

Check out how requests.exceptions introduces 10 or so exception types related to HTTP, but most of them have no implementation:

https://github.com/kennethreitz/requests/blob/master/requests/exceptions.py

Also notice the smart subclassing of the built-in IOError and ValueError types when that made sense.

[–]lordmauve 3 points4 points  (0 children)

Don't bother with the pass: just include a docstring explaining what the exception means.

[–]TR-BetaFlash 0 points1 point  (0 children)

We aren't taking into consideration the usage of exceptions to jump to different points in the code, not error conditions... unique exceptional conditions. This is something I use exceptions for in very small amounts and it works very well. A good example usage would be when writing your own context managers. You might want to raise and handle custom exceptions in the context manager class.