Size: 2553
Comment:
|
Size: 3781
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
= Manutenere le Eccezioni = |
|
Line 38: | Line 40: |
import sys | |
Line 40: | Line 43: |
except Exception, e: write_to_page( "<p>Error: %s</p>" % str(e) ) |
except: # catch *all* exceptions e = sys.exc_info()[1] write_to_page( "<p>Error: %s</p>" % e ) |
Line 44: | Line 48: |
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. | 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. |
Line 46: | Line 50: |
''umm. that snippet doesn't actually catch all exceptions, though... here a more robust solution:'' | == 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: |
Line 58: | Line 87: |
== Finding Specific Exception Names == | However, it originally was: |
Line 60: | Line 89: |
Standard exceptions that can be raised are detailed at: | {{{ #!python try: untrusted.execute() except Exception, e: write_to_page( "<p>Error: %s</p>" % str(e) ) }}} |
Line 62: | Line 97: |
http://python.org/doc/lib/module-exceptions.html | Someone pointed out that "except" catches more than just "except Exception, e." |
Line 64: | Line 99: |
Look to class documentation to find out what exceptions a given class can raise. | ''Why is that the case? What is the difference?''-- LionKimbro |
Line 66: | Line 101: |
= See Also: = | For now (version 2.3) exception doesn't have to be inherited from Exception. Thus plain 'except:' catches all exceptions, not only system. -- MikeRovner [[DateTime(2004-01-19T05:49:19Z)]] |
Line 68: | Line 103: |
WritingExceptionClasses, TracebackModule, Wiki:CoupleLeapingWithLooking | == Getting Useful Information from an Exception == |
Line 70: | Line 105: |
= To Write About... = | So, I've got something like: |
Line 72: | Line 107: |
* 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". |
{{{ #!python (a,b,c) = d }}} |
Line 77: | Line 112: |
= Questions = | ...and Python spits back: |
Line 79: | Line 114: |
(none) | {{{ ValueError: unpack list of wrong size }}} ...and so, you naturally wonder, "Well, what ''was'' in {{{d}}}?" You know- you can put a {{{print d}}} in there, and that works. But is there a better, more interesting way to get at that information that people know of? |
Handling Exceptions
Manutenere le Eccezioni
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
For now (version 2.3) exception doesn't have to be inherited from Exception. Thus plain 'except:' catches all exceptions, not only system. -- MikeRovner DateTime(2004-01-19T05:49:19Z)
Getting Useful Information from an Exception
So, I've got something like:
1 (a,b,c) = d
...and Python spits back:
ValueError: unpack list of wrong size
...and so, you naturally wonder, "Well, what was in d?"
You know- you can put a print d in there, and that works. But is there a better, more interesting way to get at that information that people know of?