522
Comment: A little something to get the ball rolling.
|
833
don't *ever* use a too general exception class, CatchWhatYouCanHandle
|
Deletions are marked like this. | Additions are marked like this. |
Line 10: | Line 10: |
except: z = "divide by zero" |
except ZeroDivisionError: print "divide by zero" }}} If you wanted to examine the exception from code, you could have: {{{ #!python (x,y) = (5,0) try: z = x/y except ZeroDivisionError, e: z = e # representation: "<exceptions.ZeroDivisionError instance at 0x817426c>" print z # output: "integer division or modulo by zero" |
Handling Exceptions
The simplest way to handle exceptions is with a "try-except" block:
If you wanted to examine the exception from code, you could have:
To Write About...
Give example of IOError, and interpreting the IOError code.
Give example of multiple excepts. Handling multiple excepts in one line.
Show how to use "else" and "finally".
Show how to continue with a "raise".
See Also:
WritingExceptionClasses, TracebackModule, CoupleLeapingWithLooking