Thread: Error Handling

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

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Error Handling


    I have been doing a beginner's tutorial for Python (using v2.7.3 on lubuntu 12.10)
    and I have encountered an error handling issue I don't understand.

    I've written the following code while playing around with the error handling to see how it works:

    loop = 1
    while loop == 1:
    try:
    a = input("Subtract this: ")
    b = input("from this: ")
    except NameError:
    print"\nChoose a number not a letter, you muppet"
    continue
    except SyntaxError:
    print"\nWhat was that gibberish? Enter a number!"
    continue
    except ZeroDivisionError:
    print"\nDivide by zero?"
    print"\nAre you trying to destroy the universe??"
    continue
    except TypeError:
    print"\nWat? I don't even..."
    continue
    print b,"-",a,"=",b-a
    try:
    loop = input("Enter '1' to continue, anything else to quit: ")
    except (NameError, SyntaxError, ZeroDivisionError, TypeError):
    loop = 0


    (by the way, in the real code all the indents are there and correct as far as I can tell - the forum seems to dislike me using indents in the post)

    Anyhow, I added 'except TypeError' because that was what I was getting if I entered 'quit' (as opposed to 'quit()') as any of the variables.
    As you can see I've attempted to handle TypeError the same way I handled the other Errors (which all function properly).

    The problem is that the program still crashes and throws up a 'TypeError' even though I have added code that should handle that... why?

    Can anyone answer this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    491
    Rep Power
    33
    How input works depends on which version of Python you are using. I am assuming Python 3.x which returns string. Generally it is something like
    Code:
    loop = 1
    while loop == 1:
        try:
            a = input("Subtract this: ")  ## assume this returns a string
            a_int = int(a)              ## assume integers and not floats
            b = input("from this: ")
            b_int = int(b)
            print b,"-",a,"=",b-a
        except:
            print "Enter numbers only"
    In the following, test for "1" and exit otherwise. There is no need for a try/except especially since you say "anything else to quit" so the user should be able to enter anything.
    Code:
        try:
            loop = input("Enter '1' to continue, anything else to quit: ")
        except (NameError, SyntaxError, ZeroDivisionError, TypeError):
            loop = 0
    Original post quoted with indents for reference. Click the hash, #, at the top and post code in between to preserve indents.
    Code:
    loop = 1
    while loop == 1:
        try:
            a = input("Subtract this: ")
            b = input("from this: ")
        except NameError:
            print"\nChoose a number not a letter, you muppet"
            continue
        except SyntaxError:
            print"\nWhat was that gibberish? Enter a number!"
            continue
        except ZeroDivisionError:
            print"\nDivide by zero?"
            print"\nAre you trying to destroy the universe??"
            continue
        except TypeError:
            print"\nWat? I don't even..."
            continue
        print b,"-",a,"=",b-a
        try:
            loop = input("Enter '1' to continue, anything else to quit: ")
        except (NameError, SyntaxError, ZeroDivisionError, TypeError):
            loop = 0
    Last edited by dwblas; January 21st, 2013 at 11:42 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    I am using Python 2.7 which unless I am mistaken returns numbers for 'input' and strings for 'raw_input'

    "There is no need for a try/except especially since you say "anything else to quit" so the user should be able to enter anything"
    In this line, entering '1' keeps the loop in play, entering another number breaks the loop. Entering a letter or a symbol causes python to **** a brick, that's why there is a 'Try'/'Except' to direct the program to break the loop (by making loop = 0).

    What I am still unclear on is why I can get the program to respond to 'NameError', 'SyntaxError', and 'ZeroDivisionError' in the manner that I choose, but I can't get it to do the same for 'TypeError'...

    (thanks for the tip on displaying code, btw)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    491
    Rep Power
    33
    Use raw_input with try/except to catch non_numbers. Code as posted above but with raw_input instead, and again assumes integers and not floats. Note that you have nothing in the code that will trigger a Zero Division, Name Error, or Syntax Error.
    Code:
    loop = 1
    while loop == 1:
        print
        try:
            a = raw_input("Subtract this: ")
            a_int = int(a)              ## non_number triggers except
            b = raw_input("from this: ")
            b_int = int(b)              ## non_number triggers except
            print "%s - %s = %d" % (b, a, b_int-a_int)
    
    
            choice = raw_input("Enter '1' to continue, anything else to quit: ")
            if choice != '1':  ## compares strings not numbers
                loop = 0
        except:
            print "Enter numbers only"
    Last edited by dwblas; January 22nd, 2013 at 06:37 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    As suggested I used raw_input to filter out non-integers. I modified my code like so:

    Code:
    ###ErrorHandling.py
    
    #Error handling using the example of a simple subtraction program
    
    #Program begins
    print"Subtracting the Error v0.2 \n\n"
    
    loop = 1
    while loop == 1:
        try:
            a = raw_input("Subtract this: ")
            b = raw_input("from this: ")
            c = int(a)
            d = int(b)
        except ValueError:
            print"\nOh no! an error occurred!"\
                       "\nDon't input letters, words, or bad equations, ok!\n\n"
            continue
        
        print d,"-",c,"=",d-c,"\n\n"
        choice = raw_input("Enter '1' to continue, anything else to quit...\n\n")
        if choice != "1":
            loop = 0
            
    #Program ends 
    print"Thank you for subtracting some time to use this program!"
    And now, as far as I can tell, I've met the tutorial's goal of making this small program uncrashable (if there's a way to crash it, please let me know).

    Though I would still like to know why a TypeError was able to crash the original iteration of the program even though I had an 'except' to deal with TypeErrors.

    Thanks for your help!

IMN logo majestic logo threadwatch logo seochat tools logo