Thread: error handling

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    2
    Rep Power
    0

    error handling


    I hope somebody could tell me how i can make a python script handling errors i want that if a error is arise that the script starts all over from then beginning and not that is is printing the error.

    thank u for helping me!
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    This example uses try/except to handle bad inputs:
    Code:
    while 1:
        ans = raw_input("Enter a number:")
        try:
            num = int(ans)
        except ValueError:
            print "Please enter only integers"
            continue
        try:
            res = 1.0/num
        except ZeroDivisionError :
            print "Zero is not allowed!"
            continue
        break
    print 1,"divided by ",num," = ",res
    Grim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    2
    Rep Power
    0
    Thank you that was helping me a lot!

IMN logo majestic logo threadwatch logo seochat tools logo