Catching errors that aren't a proper Exception

3536
6
06-21-2016 11:46 AM
BlakeTerhune
MVP Regular Contributor

More specifically, I'm using ftplib and if the server times out, my traditional except Exception as err: is not catching it. I found one solution to use except ftplib.all_errors and it seems to work but I'd like to know if there's a more generic way to catch these "other" exceptions.

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

I gave up with try except blocks for returning error messages since I found that when something fails, I just follow the error stream back to its source.  Have you really found a difference in what is meaningfully returned with and without the blocks?  The only time that I use one is if I have to delete something and if it can't I produce a new filename and move on.  So I guess my comment is, what do you want to do if something might error out... quit? or do something else?

0 Kudos
BlakeTerhune
MVP Regular Contributor

I need to catch the exception because the script is running as a scheduled task that sends an email and logs a success or failure.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What behavior are you seeing that leads you to say the errors aren't being caught?  What are you doing with the exceptions after catching them?  Logging?  Exiting?  Re-trying?

0 Kudos
BlakeTerhune
MVP Regular Contributor

There's an FTP site hosted by another organization that we routinely download data from. One of the downloads is very large and takes a while to download. The FTP server has a 120 second timeout so if the download goes too slowly for whatever reason, the script is disconnected. The error message is from ftplib and says 421 Timeout (120 seconds): closing control connection.

Our script has everything in a big try/except/finally. The main logic with a log success message is in the try, there's an except Exception as err: block that logs a failure message, then there is send email in the finally. It only logs the success or failure, then sends an email with the result.

try:
    main logic here
    log success message here
except Exception as err:
    log failure message here
finally:
    send email here

I know it's skipping the except block because the failure message is not being written to the log but I get the email. I also wrote a little test script that just connects to the ftp, checks for a file, then sleeps for 150 seconds. Again, the except Exception block does not catch the ftp timeout error.

EDIT

I just looked at the script again and I have it logging with err.message instead of just err. That might be it...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I just tested out ftplib and forced a connection timeout.  The try-except block caught the error.  The err.message was empty, but err.strerror and err.errno were populated.

BlakeTerhune
MVP Regular Contributor

This is helping me narrow it down. I have ftp.quit() in the finally block but it seems that trying to quit an ftp connection that has timed out throws the same error but as a different error object ("error_temp"). Additionally, error_temp has no attribute "strerror". I had ftp.quit() in a try/except but I was only catching AttributeError or NameError.

I think the solution to this is two-fold:

  1. Change error logging to use just err insetad of err.message.
  2. Catch (and pass) all Exceptions on ftp.quit() instead of just AttributeError or NameError.
try:
    ftp.quit()
except:
    pass
0 Kudos