In this concluding article of the Python 101 series, find out howto use Python's exception-handling routines to trap and resolve programerrors, learn to generate and use custom error messages, and get acquainteda bunch of useful (and free!) Python resources online.
You can use a single "except" statement to handle more than one error byseparating the various exception names with commas and enclosing them inparentheses. Modifying the example above, we have
#!/usr/bin/python
try:
alpha = input("Gimme a number: ")
beta = input("Gimme another number: ")
gamma = alpha / beta
print alpha, "divided by", beta, "is", gamma
except (ZeroDivisionError, OverflowError):
print "You entered an illegal value!"
else:
print "No errors encountered!"
print "-- All done --"
If you take a close look at the stack trace on the previous page, you'llsee that when Python encounters an exception, it prints both an exceptionname and a descriptive string explaining the error.
ZeroDivisionError: integer division or modulo
OverflowError: integer literal too large
This descriptive text can also be caught and used by an exception handler,if you define a variable to store it in the "except" statement. Considerthe following code:
#!/usr/bin/python
try:
alpha = input("Gimme a number: ")
beta = input("Gimme another number: ")
gamma = alpha / beta
print alpha, "divided by", beta, "is", gamma
except ZeroDivisionError, desc:
print "Illegal value (", desc, ")"
except OverflowError, desc:
print "Illegal value (", desc, ")"
else:
print "No errors encountered!"
print "-- All done --"
In this case, the "desc" variable in the exception handler stores thedescriptive error message generated by Python; this variable may then beused within the handler.
Here's the output:
Gimme a number: 10
Gimme another number: 5
10 divided by 5 is 2
No errors encountered!
-- All done --
Gimme a number: 10
Gimme another number: 0
Illegal value ( integer division or modulo )
-- All done --
Gimme a number: 897475834785348534785
Illegal value ( integer literal too large )
-- All done --
This works even if your exception handler is handling more than oneexception.
#!/usr/bin/python
try:
alpha = input("Gimme a number: ")
beta = input("Gimme another number: ")
gamma = alpha / beta
print alpha, "divided by", beta, "is", gamma
except (ZeroDivisionError, OverflowError), desc:
print "Illegal value (", desc, ")"
else:
print "No errors encountered!"
print "-- All done --"
Now, the "try" statement can only deal with exceptions that it knows about.What about the ones the developer can't predict?
Gimme a number: 76
Gimme another number: abc
Traceback (innermost last):
File "div.py", line 5, in ?
beta = input("Gimme another number: ")
File "gt;", line 0, in ?
NameError: abc
It's possible to use a general "except" statement to handle *any* type ofexception generated by the interpreter - simply omit the exception namefrom the "except" statement. The following code snippet illustrates thistechnique:
In this case, it doesn't matter what type of exception Python generates -the generic handler will catch it, ignore it and continue to process therest of the script.
Gimme a number: asd
-- All done --
Gimme a number: 10
Gimme another number: 0
-- All done --
Gimme a number: 58439058349058934859
-- All done --
Gimme a number: 10
Gimme another number: 2
10 divided by 2 is 5
-- All done --
It should be noted, however, that this approach, although extremely simple,is *not* recommended for general use. It is poor programming practice totrap all errors, regardless of type, and ignore them; it is far better -and more professional - to anticipate the likely errors ahead of time, anduse the "try-except" construct to isolate and resolve them.