Best Way to Handle All Exceptions
Carl Banks
pavlovevidence at gmail.com
Mon Jul 13 20:49:23 EDT 2009
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.
Just because something is usually a bad idea doesn't mean it always
is.
Carl Banks
More information about the Python-list
mailing list