Python: How can I know which exceptions might be thrown from a method call

前端 未结 7 1271
感情败类
感情败类 2020-11-28 05:43

Is there a way knowing (at coding time) which exceptions to expect when executing python code? I end up catching the base Exception class 90% of the time since I don\'t kno

相关标签:
7条回答
  • 2020-11-28 06:14

    Noone explained so far, why you can't have a full, 100% correct list of exceptions, so I thought it's worth commenting on. One of the reasons is a first-class function. Let's say that you have a function like this:

    def apl(f,arg):
       return f(arg)
    

    Now apl can raise any exception that f raises. While there are not many functions like that in the core library, anything that uses list comprehension with custom filters, map, reduce, etc. are affected.

    The documentation and the source analysers are the only "serious" sources of information here. Just keep in mind what they cannot do.

    0 讨论(0)
提交回复
热议问题