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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.