Question

Where can I write my custom exceptions?

Is there a file like execeptions.py in my software, or do I have to write them in the class they are related to?

Are there any PEPs about that?

Was it helpful?

Solution

in general i find that i have two kinds of exception.

one is intended for a very specific error, and is only thrown in one part of the code. in that case, i define the exception close to where it is used. this way, when a developer sees the exception, and searches the code for it, they also find the cause (and hopefully, in comments, some helpful documentation).

the other is an exception thrown multiple places across a library - a kind of "this library has failed" exception. and then i define it in a top level module for the library (in whatever module the user is most likely to use as the "main entry point" for the system).

sometimes the first kind can subclass the second.

this is just my own use - i don't know of any peps or other guidelines.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top