How do you find what exceptions a class can throw?

Avi Gross avigross at verizon.net
Mon Dec 21 16:22:02 EST 2020


I agree Chris, that your original question is quite to the point. But is it always?

If you do something that tries to read a file (such as opening a database to send queries) on a remote server, how many different things might time out for various reasons? Some may resolve if you keep trying and some attempts may make it worse if everybody is pounding away resulting in a denial of service attack scenario.

Some people dealing with situations come up with more detailed attempts such as choosing a random interval before trying again. Protocols like the Post Office Protocol often already have built-in techniques like that in the protocol and the timeout may be caused by something in your chain not being set to wait long enough. 

So your attempt to retry a few times may well work. I wonder if a backup method would be to switch to using IMAP as many mail archives now support a way to approach them using several such interfaces, albeit perhaps to different destinations and other changes. But hat is not a trivial change to use as just an alternate. Trying again may work, albeit trying 100 times per second may not help if a server is rebooting or is out of memory or file space or other resources ...

I will say that if your function calls A() which calls B() and so on and the timeout happens at any place long the way or even at Z() then it is quite possible you may not get a specific exception back. Z() may indeed send back a TIMEOUT1 exception but then E() may intercept it and return a GENERIC_FAILURE12 exception. Then B() may retry a few times and finally return a I_GIVE_UP_TRY LATER123 exception. You likely are depending on a module you have no control over that uses other modules.

But in your case, it may indeed be that simple.

Just a thought. You can write some code that catches every possible exception then carefully logs all details, and then perhaps quits. If you run your code many times and even cause reasons for it to fail, you may develop some knowledge of some of the exceptions that make it through back to your call and then you can change your code to specifically handle only those you have some idea what to do about. As mentioned, you have traceback info that may help you figure out where the exceptions come from or even why.

Good luck.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of Chris Green
Sent: Monday, December 21, 2020 4:06 AM
To: python-list at python.org
Subject: Re: How do you find what exceptions a class can throw?

Avi Gross <avigross at verizon.net> wrote:
> The original question sounded like someone was asking what errors 
> might be thrown for a routine they wrote that used other components 
> that might directly throw exceptions or called yet others, ad nauseum.
> 
OP here.  The original question was because I wanted to trap timout errors when connectiing to a POP3 server in a very simple little program that collects mail and runs it through a filter before delivering it.

Thus I simply needed to know what the 'name' of the exception was so I could trap only timeouts.  Timeout errors are (probably) 'soft' errors which merit a retry, most other errors when trying to connect to a POP server would be unrecoverable (POP syntax, wrong name, wrong password, etc.).

It didn't seem such a difficult question when I asked it! :-)

--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list