4.83. try

Allows you to trap errors. Interchange processes the body text of the [try][/try] block and returns it normally if it does not generate an error. If it does generate an error, Interchange executes the [catch][/catch] block.

See also 'catch'.

4.83.1. Summary

    [try label=my_label other_named_attributes]
        Body text to return if no error
    [/try]
    .
    .
    .
    [catch my_label]
        Body text to return if try block caused an error
    [/catch]
Parameters Description Default
label The label shared by the paired try and catch blocks 'default'
Attributes Description Default
status Returns 0 (failed) or 1 (succeeded) instead of page output none
hide Suppresses page output No
clean Suppress try block output if it has an error No
interpolate See Interpolating Parameters No
reparse See Interpolating Parameters Yes
Other_Characteristics  
Invalidates cache No
Container tag Yes

Tag expansion example:

    [set divisor]0[/set]
    [try label=div]
        [calc] 1 / [scratch divisor] [/calc]
    [/try]
    [catch div]Division error[/catch]
---
    Division Error

ASP-like Perl call:

    $Tag->try(    { label  => I<'try_catch_label'>,
                    status => 1, },
                  $try_body  );

    $Tag->catch(  { label  => I<'try_catch_label'>, },
                  $catch_body )

or similarly with positional parameters,

    $Tag->try($label, $attribute_hash_reference, $try_body);
    $Tag->catch($label, $attribute_hash_reference, $catch_body);

4.83.1.1. See Also

catch

4.83.2. Description

Allows you to trap errors. Interchange processes the body text of the [try][/try] block and returns it normally if it does not generate an error. If it does generate an error, interchange executes the [catch][/catch] block. The catch block executes where it is on the page (i.e., it does not replace the output of the try block).

Note that the catch block must occur after the [try] block in the document.

4.83.2.1. label

The try and catch blocks are matched by this label.

Technical note:

The try tag will also place a result in the $Session object. For example, the following returns the 'Illegal division by zero...' error message if it occurs:

    [try label=divide][calc] 1 / [scratch divisor] [/calc][/try]

    [catch divide]
        [calc]$Session->{try}{divide}[/calc]
    [/catch]

The $Session->{try}{divide} object will be set to the empty string ('') if there was no error, or it will contain the error message if there was an error.

The [perl] and [calc] tags also set $Session->{try}->{active_label} on errors.

4.83.2.2. status

Suppresses try block output and returns 1 if no error or 0 if an error occurred instead. Executes the catch block as usual in case of an error.

4.83.2.3. hide

Suppresses try block output (regardless of success or failure). Executes the catch block as usual in case of an error.

4.83.2.4. clean

Setting 'clean=1' will cause the try block to suppress its output only if it has an error. Otherwise (clean=0 or not set), the try block will return whatever partial output it has completed before the error. The catch block will work as usual.