Differences between revisions 7 and 8
Revision 7 as of 2003-11-22 00:02:44
Size: 1274
Editor: ip503dabc3
Comment: CatchWhatYouCanHandle
Revision 8 as of 2003-11-22 05:10:39
Size: 1992
Editor: dsl254-010-130
Comment: When you want to catch all exceptions.
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
This page previously used a catch-all exception clause: == General Error Catching ==

Sometimes, you want to catch ''all'' errors that could possibly be generated.

''In most cases, you don't.'' In most cases, you want to be as specific as possible. If a user presses Ctrl-C, generating a KeyboardInterrupt, you don't want the program to interpret it as a File I/O error. (Wiki:CatchWhatYouCanHandle)

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 code something like this:
Line 30: Line 40:
(x,y) = (5,0)
Line 32: Line 41:
  z = x/y
except:
  print "divide by zero"
  untrusted.execute()
except Exception, e:
  write_to_page( "<p>Error: %s</p>" % str(e) )
Line 37: Line 46:
This is bad because you'll almost certainly catch too many errors. As a simple example, when the user presses Ctrl-C, thus generating a KeyboardInterrupt, it will be caught and the code above will print "divide by zero". Now that's confusing, so you should only Wiki:CatchWhatYouCanHandle. 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 52: Line 61:

= Questions =

(put your question here)

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.

In most cases, you don't. In most cases, you want to be as specific as possible. If a user presses Ctrl-C, generating a KeyboardInterrupt, you don't want the program to interpret it as a File I/O error. (CatchWhatYouCanHandle)

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 code something like this:

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

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

(put your question here)

HandlingExceptions (last edited 2020-09-14 13:35:48 by MatsWichmann)

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