try: Handles exceptions

The try tag allows exception handling in DTML, mirroring the Python try/except and try/finally constructs.


The try tag has two different syntaxes, try/except/else and try/finally.

try/except/else Syntax:

        <dtml-except [ExceptionName] [ExceptionName]...>

The try tag encloses a block in which exceptions can be caught and handled. There can be one or more except tags that handles zero or more exceptions. If an except tag does not specify an exception, then it handles all exceptions.

When an exception is raised, control jumps to the first except tag that handles the exception. If there is no except tag to handle the exception, then the exception is raised normally.

If no exception is raised, and there is an else tag, then the else tag will be executed after the body of the try tag.

The except and else tags are optional.

try/finally Syntax:


The finally tag cannot be used in the same try block as the except and else tags. If there is a finally tag, its block will be executed whether or not an exception is raised in the try block.


Zero or more exception names. If no exceptions are listed then the except tag will handle all exceptions.

Tag Variables

Inside the except block these variables are defined.

The exception type.
The exception value.
The traceback.


Catching a math error:

        <dtml-var expr="1/0">
      <dtml-except ZeroDivisionError>
        You tried to divide by zero.

Returning information about the handled exception:

        <dtml-call dangerousMethod>
        An error occurred.
        Error type: <dtml-var error_type>
        Error value: <dtml-var error_value>

Using finally to make sure to perform clean up regardless of whether an error is raised or not:

      <dtml-call acquireLock>
        <dtml-call someMethod>
        <dtml-call releaseLock>

See Also

raise tag

Python Tutorial: Errors and Exceptions

Python Built-in Exceptions