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

Viewed 22698

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 know which exception type might be thrown (reading the documentation doesn't always help, since many times an exception can be propagated from the deep. And many times the documentation is not updated or correct).

Is there some kind of tool to check this (like by reading the Python code and libs)?

7 Answers

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.

Related