-2

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?

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
tapioco123
  • 3,235
  • 10
  • 36
  • 42
  • Why are you asking all of these abstract style questions? – Marcin Apr 15 '12 at 13:35
  • Because I want to follow PEP and generally write good code, is this a problem for you? – tapioco123 Apr 15 '12 at 13:50
  • 3
    Then read the peps. Don't post questions here with no research. – Marcin Apr 15 '12 at 13:57
  • No, there isn't (AFAIK) a PEP about where you define exceptions or write unit tests. Put them where it makes sense for your project. – Thomas K Apr 15 '12 at 14:01
  • 1
    @tapioco123 You know that it is possible to find PEPs without asking SO, right? – Marcin Apr 15 '12 at 14:54
  • 1
    @andrewcooke Actually no, the good questions cannot be solved with *a little* effort and google. – Marcin Apr 15 '12 at 15:20
  • 3
    @andrewcooke I believe that every time someone asks such question it's worth to point in out to them. After all, "search on google for me" is way worse than "write the code for me", "debug the code for me" an such, all of which is generally against the site rules. – Lev Levitsky Apr 15 '12 at 15:21
  • 1
    ok I googled it for you: a [PEP](http://www.python.org/dev/peps/) isn't going to tell you how to write exceptions, since they are an ancient part of the language and not an _enhancement_ proposal. There's a difference between language enhancements, style guidelines, and basic python. I suggest you look into the later two – Shep Apr 15 '12 at 15:31

1 Answers1

1

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.

andrew cooke
  • 45,717
  • 10
  • 93
  • 143