ExceptionsThe try statement
try:
f = open("foo")
except IOError:
print "Couldn't open 'foo'. Sorry."
The raise statement
def factorial(n):
if n < 0:
raise ValueError,"Expected non-negative number"
if (n <= 1): return 1
else: return n*factorial(n-1)
Uncaught exceptions>>> factorial(-1) Traceback (innermost last): File "<stdin>", line 1, in ? File "<stdin>", line 3, in factorial ValueError: Expected non-negative number >>> |
| <<< | O'Reilly OSCON 2000, Introduction to Python, Slide 20 July 17, 2000, beazley@cs.uchicago.edu |
>>> |