522
Comment: A little something to get the ball rolling.
|
2312
further question
|
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" |
Line 13: | Line 13: |
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" }}} == General Error Catching == Sometimes, you want to catch ''all'' errors that could possibly be generated, but usually ''you don't''.In most cases, you want to be as specific as possible (Wiki:CatchWhatYouCanHandle). In the first example above, if you were using a catch-all exception clause and a user presses Ctrl-C, generating a KeyboardInterrupt, you don't want the program to print "divide by zero". However, there are some situations where it's best to catch ''all'' errors. For example, suppose you are writing an extension module to a web service. You want the error information to output the output web page, and the server to continue to run, if at all possible. But you have no idea what kind of errors you might have put in your code. In situations like these, you may want to code something like this: {{{ #!python try: untrusted.execute() except Exception, e: write_to_page( "<p>Error: %s</p>" % str(e) ) }}} MoinMoin software is a good example of where this is done. If you write Moin``Moin extension macros, and trigger an error, Moin``Moin will give you a detailed report of your error and the chain of events leading up to it. |
|
Line 27: | Line 59: |
= Questions = Is there an easy way to find all of the exceptions, and parameters to the exceptions, that a class has? -- LionKimbro I'm not sure what you mean. Do you want to know what exceptions a class can raise or what parameters an exception class can take when raised? -- JohannesGijsbers |
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:
General Error Catching
Sometimes, you want to catch all errors that could possibly be generated, but usually you don't.In most cases, you want to be as specific as possible (CatchWhatYouCanHandle). In the first example above, if you were using a catch-all exception clause and a user presses Ctrl-C, generating a KeyboardInterrupt, you don't want the program to print "divide by zero".
However, there are some situations where it's best to catch all errors.
For example, suppose you are writing an extension module to a web service. You want the error information to output the output web page, and the server to continue to run, if at all possible. But you have no idea what kind of errors you might have put in your code.
In situations like these, you may want to code something like this:
MoinMoin software is a good example of where this is done. If you write MoinMoin extension macros, and trigger an error, MoinMoin will give you a detailed report of your error and the chain of events leading up to it.
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
Questions
Is there an easy way to find all of the exceptions, and parameters to the exceptions, that a class has? -- LionKimbro
I'm not sure what you mean. Do you want to know what exceptions a class can raise or what parameters an exception class can take when raised? -- JohannesGijsbers