Exceptions

The 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 exception

     >>> 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, Advanced Python Programming, Slide 17
July 17, 2000, beazley@cs.uchicago.edu
>>>