Home arrow Zope arrow Page 5 - Exception Handling In DTML

All For One... - Zope

Ever wondered if there was a way to stop Zope from barfing error messages all over your screen when one of your scripts crashes and burns? Well, guess what - there is! Meet the and tags, which can be used to trap and resolve errors in script execution.

  1. Exception Handling In DTML
  2. Anatomy Class
  3. Playing Catch
  4. Being Verbose
  5. All For One...
  6. ...And One For All
  7. The Final Solution
  8. Raising The Bar
  9. Pre-Packaged Python
  10. Endzone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 1
August 13, 2002

print this article


You can also write specific exception handlers for different types of exceptions, by noting the exception type within the tag. Consider the following handler, which only handles KeyError exceptions:

<dtml-try> <dtml-var CheckThisOut> <dtml-except KeyError> Undefined variable - <i><dtml-var error_value></i> </dtml-try>
Here's the output:

Undefined variable - CheckThisOut
Of course, this handler will now only restrict its activities to KeyError exceptions. All other errors will be routed via the default Zope error handler.

You can trap more than one exception, and handle each one in a different way, by using multiple <dtml-except> statements within a single <dtml-try> block.

<dtml-try> execute this block<dtml-except err1>execute this block if exception "err1" is raised<dtml-except err2>execute this block if exception "err2" is raised... and so on ...<dtml-else>execute this block if no exceptions are raised</dtml-try>
If an exception is encountered while running the code within the <dtml-try> block, Zope stops execution of the <dtml-try> block at that point and begins checking each <dtml-except> block to see if there is a handler for the exception. If a handler is found, the code within the appropriate <dtml-except> block is executed; if not, control either moves to the parent <dtml-try> block, if one exists, or to the default handler.

Once the <dtml-try> block has been executed and assuming that the program has not been terminated, the lines following the <dtml-try> block are executed.

In case you're wondering, the <dtml-else> branch in the block above is executed only if no exceptions are raised. It's optional, though, so only include it if you need to.

Take a look at the next example, which demonstrates how this works:

<dtml-try> <dtml-var alpha> divided by <dtml-var beta> is <dtml-varexpr="_.int(alpha)/_.int(beta)" >.<br><dtml-except KeyError><b>No value assigned to the variable: <dtml-var error_value></b><dtml-except ZeroDivisionError><b>Division by zero</b><dtml-except ValueError><b>Illegal value: <dtml-var error_value></b><dtml-else><i>No errors encountered.</i></dtml-try>
Save this code in a DTML Document named "ExceptionallyYours", and test it by passing it values for the variables "alpha" and "beta" on the URL string, via the GET method (you can also use a form and submit values for these variables via POST). For example, if you access the URL

you'll see

12 divided by 1 is 12. No errors encountered.
But look what happens when you try

Since no values have been passed for the variables, Zope barfs with a KeyError, which is caught by the exception handler, and the following message is displayed:

No value assigned to the variable: alpha
What about if you pass an illegal value, like a very large integer? Try the following URL,

and then see what Zope says:

Illegal value: int() literal too large: 12732132132
Finally, try attempting division by zero,

and watch as Zope generates a ZeroDivisionError, and an appropriate message.

Division by zero
If you'd like to save yourself some keystrokes, you also have the option of handling multiple exceptions within a single <dtml-except> block, by specifying a list of exception types separated by spaces. Consider the following rewrite of the previous example, which uses the same exception handler for both ZeroDivisionError and ValueError exceptions:

<dtml-try> <dtml-var alpha> divided by <dtml-var beta> is <dtml-varexpr="_.int(alpha)/_.int(beta)" >.<br><dtml-except KeyError><b>No value assigned to the variable: <dtml-var error_value></b><dtml-except ValueError ZeroDivisionError><b>Illegal value: <dtml-var error_value></b><dtml-else><i>No errors encountered.</i></dtml-try>

>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: