documenting excepetions in Python

Bruno Desthuilliers bdesth.quelquechose at
Fri Oct 19 22:27:25 CEST 2007

dale_bertrand at a écrit :
> In python, how do I know what exceptions a method


A method is only a thin wrapper around a function, and functions are 
just one kind of callable object (classes are another, and you can 
define your own...)

> could raise?

Practically speaking, you can't. Since an unhandled exception bubbles up 
the call stack, there's no reliable way to know what exception could 
happen when calling a function. Now there are quite a lot of 
'exceptions' you can expect in some situations - like IOError when 
dealing with files, etc.

>  Do I
> need to look at the source?

Would be impractical. Trying to spot each and every exception that could 
happen in a real-world call stack is a waste of time IMHO. Just deal 
with the ones that:
1/ could obviously happen here (like : a missing key in a dict, a 
non-(existing|readable|writable) file, etc,
2/ you can handle at this level

Else, just learn to live with the fact that shit happens.

>  I don't see this info in the API docs for
> any of the APIs I'm using.

Indeed. Most of the time, it would be just meaningless.

More information about the Python-list mailing list