6 <:try:>, <:except:>, <:else:>, <:finally:>

<:try:>
    <:except:>
    [ <:else:> ]
<:/try:>

or

<:try:>
    <:except [exc]:>
    [ <:except [exc]:> ]
    [ <:except:> ]
    [ <:else:> ]
<:/try:>

or

<:try:>
    <:finally:>
<:/try:>

This tag executes everything inside of it up until it first sees an <:except:> or <:finally:> tag. If an exception (error) occurs during that execution, this tag will then take one of the following actions:

  1. If there is an <:except:> tag which specifies the exception in its exc argument, it will execute the stuff occurring after that <:except:> tag until it meets another <:except:> tag, an <:else:> tag, or the end of the <:try:> tag.

  2. Otherwise, if there is an <:except:> tag with nothing specified in exc, it executes the stuff after that <:except:> tag.

  3. Otherwise, there is nothing to catch the exception, so it ``propagates'' upward to another containing <:try:> block. If there is no containing <:try:> tag, SkunkWeb generates a page error.

If you have an <:else:> tag after all of your <:except:> tags, the stuff after <:else:> will be executed if no exception has occurred.

If you wish to specify particular exceptions in an <:except:> tag, use a backticked expression with the exception object. If you want to specify more than one exception for an <:except:> tag, specify them as a tuple expression.

Example:

<:try:>
    <:call `something_which_will_blow_up()`:>
<:except `KeyError`:>
    We got a KeyError. 
<:except `(TypeError, ValueError)`:>
    We got a TypeError or ValueError.
<:except:>
    We got some other error. 
    But I know it wasn't a KeyError, TypeError, or ValueError!
<:else:>
    Hey, no error!!!!
<:/try:>

All of this behavior is identical to the way that try:, except: and else: work in regular Python. Read the Python documentation about exceptions for more info.

Note the last way to use the <:try:> tag: with a <:finally:> tag. The tags behave the same way as Python's try: and finally: statements.

CAVEAT PROGRAMMER: beware of the <:except:> tag with no arguments, as you may catch the document timeout exception (requestHandler.requestHandler.DocumentTimeout). If you catch it and ignore it, your document will never time out. Moral of the story, either:

  1. Don't use the <:except:> tag without arguments
  2. Make sure you don't do anything in your <:try:> block that may block.

FLASH! As of SkunkWeb 3.2.3, the document timeout exception will be reraised every second after a timeout occurs, so while you still need to be careful (say you have an operation that times out, wrapped in a try: block with a catchall except: in a while loop), you don't have to be quite as careful.