Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Learn to Program Using Python: A Tutorial for Hobbyists, Self-Starters, and All    (ISBN: 0201709384)


 

 List Price: $32.95
 Our Price: $23.07
 Used Price: $11.95

 Release Date: 15 January, 2001
 Manufacturer: Addison-Wesley Pub Co (Paperback)
 Sales Rank: 45,440

 Author: Alan Gauld









More Info

 Python Exception Handling 2003-02-03 13:14:12
  python 
Category: source:python:general
Description: An example on handling errors with try and except.
Platform: all
Author: mind
Viewed: 7569
Rating: 3.7/5 (33 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
exception.py
#!/usr/local/bin/python
#
# Errors And Exceptions by mind@metalshell.com
#
# an example on handling errors, with try/except
#
# 03/12/2002
#
# http://www.metalshell.com
#

# When errors occur you would normally see something like:
#
# Traceback (most recent call last):
#   File "./errors.py", line 12, in ?
#     x = int(raw_input("Enter a number: "))
# ValueError: invalid literal for int(): a
#
# when an error in your code is caught your program will be
# terminated but if you try and except things you can prevent
# your program from ending by just placing a nice reminder to
# the user to try another option or value..
#


# the below example shows a good way to catch errors, for example
# we are making x an integer that will store whatever the user enter's
# so if the user entered 'a' obviously it isn't an integer so it
# would produce an error.. So here we are trying to store the user
# input into x as an integer and make an exception for an error..

# also our second exception we are going to parse out user termination
# signals such as "Ctrl+C"..

while 1:
        try:
                print "Try entering an alphabetic character 'a b..'"

                # store the user-input into x as an integer
                x = int(raw_input("Enter a number: "))

                # if raw_input() doesn't receive any errors then
                # break the while loop, otherwise.. print error..
                break

        except ValueError:
                print
                print "Please enter a valid number [0-9]"


# in this example i'll show you how you can use try/except to parse
# out keyboard/user related termination signals such as ctrl+c
# the only way to terminate this program is manually kill the process
# or enter the correct information it asks for, other than that the
# while loop will continue on looping..

secret="errorsExample"
print "The secret answer is (%s) without brackets, but try to terminate" %(secret)
print "the program by using termination signals like, ctrl+c, ctrl+d, etc.."

while 1:
        try:
                q = raw_input("Enter secret answer: ")
                if q == secret:
                        print "You enetered the right secret answer!"
                        break

        except KeyboardInterrupt:
                print
                print "Caught ctrl + c"
                pass

        except EOFError:
                print
                print "Caught ctrl + d"
                pass
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?