Differences between revisions 1 and 18 (spanning 17 versions)
Revision 1 as of 2003-11-17 20:12:31
Size: 522
Editor: dsl254-010-130
Comment: A little something to get the ball rolling.
Revision 18 as of 2004-01-19 05:49:19
Size: 3325
Editor: MikeRovner
Comment:
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 14: Line 14:
== To Write About... == If you wanted to examine the exception from code, you could have:
Line 16: Line 16:
Give example of IOError, and interpreting the IOError code. {{{
#!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"
}}}
Line 18: Line 26:
Give example of multiple excepts. Handling multiple excepts in one line. == General Error Catching ==
Line 20: Line 28:
Show how to use "else" and "finally". 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".
Line 22: Line 30:
Show how to continue with a "raise". 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
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.
Line 26: Line 58:
WritingExceptionClasses, TracebackModule, Wiki:CoupleLeapingWithLooking 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
try:
  untrusted.execute()
except Exception, e:
  write_to_page( "<p>Error: %s</p>" % str(e) )
}}}

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)]]

Handling Exceptions

The simplest way to handle exceptions is with a "try-except" block:

   1 (x,y) = (5,0)
   2 try:
   3   z = x/y
   4 except ZeroDivisionError:
   5   print "divide by zero"

If you wanted to examine the exception from code, you could have:

   1 (x,y) = (5,0)
   2 try:
   3   z = x/y
   4 except ZeroDivisionError, e:
   5   z = e # representation: "<exceptions.ZeroDivisionError instance at 0x817426c>"
   6 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 (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:

   1 import sys
   2 try:
   3   untrusted.execute()
   4 except: # catch *all* exceptions
   5   e = sys.exc_info()[1]
   6   write_to_page( "<p>Error: %s</p>" % e )

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:

   1 import sys
   2 try:
   3   untrusted.execute()
   4 except: # catch *all* exceptions
   5   e = sys.exc_info()[1]
   6   write_to_page( "<p>Error: %s</p>" % e )

However, it originally was:

   1 try:
   2   untrusted.execute()
   3 except Exception, e:
   4   write_to_page( "<p>Error: %s</p>" % str(e) )

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)

HandlingExceptions (last edited 2024-03-05 20:29:39 by MatsWichmann)

Unable to edit the page? See the FrontPage for instructions.