Size: 3281
Comment: response
|
Size: 3130
Comment: (?) except Exception, e: vs. except:
|
Deletions are marked like this. | Additions are marked like this. |
Line 38: | Line 38: |
import sys try: untrusted.execute() except: # catch *all* exceptions e = sys.exc_info()[1] write_to_page( "<p>Error: %s</p>" % e ) }}} MoinMoin software is a good example of where general error catching is good. 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. Python software needs to be able to catch ''all'' errors, and deliver them to the recipient of the web page. == Finding Specific Exception Names == Standard exceptions that can be raised are detailed at: http://python.org/doc/lib/module-exceptions.html Look to class documentation to find out what exceptions a given class can raise. = See Also: = On this wiki: WritingExceptionClasses, TracebackModule. For general (non-Python specific) ideas about exceptions, consult Wiki:ExceptionPatterns. = 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". = Questions = == General Error Handling == In the "general error handling" section above, it says to catch all exceptions, you use the following code: {{{ #!python import sys try: untrusted.execute() except: # catch *all* exceptions e = sys.exc_info()[1] write_to_page( "<p>Error: %s</p>" % e ) }}} However, it originally was: {{{ #!python |
|
Line 44: | Line 95: |
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. | Someone pointed out that "except" catches more than just "except Exception, e." |
Line 46: | Line 97: |
== To Write About... == | Why is that the case? What is the difference? |
Line 48: | Line 99: |
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, Wiki: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 Both, actually. I'm thinking: "I'm writing some code, and I want to be reasonably aware of things that could go wrong, that I might not think of otherwise. Then, I want to know the parameters for the exceptions, when I learn what could go wrong." I can look up the "dict" builtin class in the Python manual, but it doesn't say, right there, that it emits the K''''''eyError. Now, you and I know about the K''''''eyError, but there are times when I need the exact word of the exception, and I want to know the parameters. So, where to people find it? Or is there just no such easy way to look up, yet? -- LionKimbro [[DateTime(2003-11-23T21:25:28Z)]] Well, there's http://python.org/doc/lib/module-exceptions.html for all the standard exceptions that can be raised. As to what exceptions a class can raise, it should be in the documentation for that class, as it is for the dict: http://python.org/doc/current/lib/typesmapping.html, note 1. -- JohannesGijsbers |
-- LionKimbro |
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 general error catching is good. 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. Python software needs to be able to catch all errors, and deliver them to the recipient of the web page.
Finding Specific Exception Names
Standard exceptions that can be raised are detailed at:
Look to class documentation to find out what exceptions a given class can raise.
See Also:
On this wiki: WritingExceptionClasses, TracebackModule.
For general (non-Python specific) ideas about exceptions, consult ExceptionPatterns.
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".
Questions
General Error Handling
In the "general error handling" section above, it says to catch all exceptions, you use the following code:
However, it originally was:
Someone pointed out that "except" catches more than just "except Exception, e."
Why is that the case? What is the difference?
-- LionKimbro