How Can I Find a List of All Exceptions That a Given Library Function Throws in Python?

前端 未结 4 1807
忘了有多久
忘了有多久 2021-01-11 10:16

Sorry for the long title, but it seems most descriptive for my question.

Basically, I\'m having a difficult time finding exception information in the official python

4条回答
  •  难免孤独
    2021-01-11 10:52

    To amplify Messa, catch what you expect are failure modes that you know how to recover from. Ian Bicking wrote an article that addresses some of the overarching principles as does Eli Bendersky's note.

    The problem with the sample code is that it is not handling errors, just prettifying them and discarding them. Your code does not "know" what to do with a NameError and there isn't much it should do other than pass it up, look at Bicking's re-raise if you feel you must add detail.

    IOError and OSError are reasonably "expectable" for a shutil.move but not necessarily handleable. And the caller of your function wanted it to move a file and may itself break if that "contract" that Eli writes of is broken.

    Catch what you can fix, adorn and re-raise what you expect but can't fix, and let the caller deal with what you didn't expect, even if the code that "deals" is seven levels up the stack in main.

提交回复
热议问题