How do you find what exceptions a class can throw?

Julio Di Egidio julio at diegidio.name
Sun Dec 20 13:12:49 EST 2020


On Sunday, 20 December 2020 at 18:18:26 UTC+1, Chris Green wrote:

> If I ignore the exception then the 
> program just exits, if I want the program to do something useful about 
> it (like try again) then I have to catch the specific exception as I 
> don't want to try again with other exceptions.

As other have hinted at, it's about handling, not so much about catching.

That said, the docs are your friend:

<https://docs.python.org/3.8/library/poplib.html#poplib.error_proto>
"exception poplib.error_proto -- Exception raised on any errors from this module (errors from socket module are not caught) [where "error_proto" I suppose stands for "protocol error"]. The reason for the exception is passed to the constructor as a string."  (It's also documented in the code docs...)

So that's one exception and the only explicit one (see below) specific to that module.  Then you should check the exceptions in the socket module:
<https://docs.python.org/3.8/library/socket.html#socket.error>

Incidentally, I have peeked at the source code for poplib, and the initializer of poplib.POP3_SSL also raises ValueError on invalid arguments.  I suppose similar should be expected from the socket module.  But these are the exceptions that typically are not handled: one has to validate input, so that, at that point, a ValueError, or a TypeError (or a KeyError, etc.) is rather a bug.  Anyway, this is another story...

Julio


More information about the Python-list mailing list