#1
  1. Call of Booty
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    252
    Rep Power
    11

    Exception Handling


    I'm new to Python, well versed in PHP and a bit of Perl.

    I've written a simple backup utility that FTPs from one server to another. I'd like to implement exception handling on the FTP should someting go wrong.

    This is basically what I have so far:
    Code:
    try:
      	ftp = FTP(ftp_host, ftp_user, ftp_pass)
      	ftp.storbinary('STOR ' + filename, pointer)
      	ftp.close()
      except:
    I'd like to use 'all_errors' documented here.html but I don't know how! Will I have to create a handler for each other exception?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    Lawrence, Kansas [KU]
    Posts
    1,559
    Rep Power
    15
    Code:
    import ftplib
    try:
    	ftp = ftplib.FTP('ftp.cwi.nl')   # change to invalid address to test
            # user anonymous, passwd anonymous@
    	ftp.login()               
    	ftp.retrlines('LIST')     # change from LIST to invalid command to test
    except ftplib.all_errors:
    	print "an error occured" #error message for any error.

    here you go. make the changes to the right to test it out

    Comments on this post

    • netytan agrees : Nice example :).
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Why use except ftplib.all_errors: when just plain old except is doing the same thing? Or can except ftplib.all_errors print out specific errors?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by †Yegg†
    Why use except ftplib.all_errors: when just plain old except is doing the same thing? Or can except ftplib.all_errors print out specific errors?
    A bare except does NOT do the same thing, as it will catch errors which it shouldn't (which is why, except in rare cases such top-level error handling/logging, you should not use them). For instance:
    Code:
    try:
        abc = 1
        abc = do_something_with(acb)
    except:
        abc = 4
    will always make abc 4. it would be much better if NameError were raised at this point. Bare excepts lead to subtle hard to track errors.

    --OH.

    Comments on this post

    • jacktasia agrees
  8. #5
  9. Call of Booty
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    252
    Rep Power
    11
    I wanted to email myself the error when/if it happened, so this is what I ended up with:
    Code:
    try:
       	ftp.connect(ftp_host)
       	ftp.login(ftp_user, ftp_pass)
       	ftp.storbinary("STOR " + filename, pointer)
       	ftp.close()
       except Exception, e:
       	err_msg = "Headers and stuff here"
       	for arg in e.args:
       		err_msg = err_msg + str(arg) + "\n"
       
       	from smtplib import SMTP
       	mFrom = 'alert@*.com'
       	mTo   = myemail@***.com
       
       	mailer = SMTP('mail.***.com')
       	mailer.sendmail(mFrom, mTo, err_msg)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by IceNine
    I wanted to email myself the error when/if it happened, so this is what I ended up with:
    Code:
        err_msg = "Headers and stuff here"
        for arg in e.args:
            err_msg = err_msg + str(arg) + "\n"
    That code won't work properly unless there's only one arg in e.args. If there's only one, use string formats:
    Code:
        err_header = "Header text\n"
        err_body = "body intro: %s\n"%(e.args[0])
        err_msg = err_header + err_body
    If there's more than one, use a list comp and string formats:
    Code:
        err_header = "Header text"
        err_body = ["body intro: %s"%(arg)
                    for arg in e.args]       
        err_msg = err_header + "".join(err_body)
    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo