Best Way to Handle All Exceptions

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Jul 13 23:25:52 EDT 2009


On Mon, 13 Jul 2009 17:49:23 -0700, Carl Banks wrote:

> On Jul 13, 12:31 pm, Piet van Oostrum <p... at cs.uu.nl> wrote:
>> >>>>> seldan24 <selda... at gmail.com> (s) wrote:
>> >s> Hello,
>> >s> I'm fairly new at Python so hopefully this question won't be too s>
>> >awful.  I am writing some code that will FTP to a host, and want to s>
>> >catch any exception that may occur, take that and print it out s>
>> >(eventually put it into a log file and perform some alerting action).
>> >s> I've figured out two different ways to do this, and am wondering
>> >which s> is the best (i.e. cleanest, 'right' way to proceed).  I'm
>> >also trying s> to understand exactly what occurs for each one. s> The
>> >first example:
>> >s> from ftplib import FTP
>> >s> try:
>> >s>     ftp = FTP(ftp_host)
>> >s>     ftp.login(ftp_user, ftp_pass) s> except Exception, err:
>> >s>     print err
>>
>> I think you should restrict yourself to those exceptions that are
>> related to ftp. Do you want to catch an exception like a misspelling in
>> one of the variables?
> 
> He quite reasonably could want that, such as if the program is designed
> to be run from a cron job, or in some other non-interactive way.


Why is it okay for non-interactive programs to silently have incorrect 
behaviour?

What's the point of a cron job that doesn't do what it is supposed to, 
because it has a bug which is silently swallowed?



"I find it amusing when novice programmers believe their main job is 
preventing programs from crashing. ... More experienced programmers 
realize that correct code is great, code that crashes could use 
improvement, but incorrect code that doesn't crash is a horrible 
nightmare."

http://www.pphsg.org/cdsmith/types.html




-- 
Steven



More information about the Python-list mailing list