try
If a runtime error is detected in the [try]...[/try] block,
then Interchange will execute the correspondingly-labelled
[catch]...[/catch] block.
Summary
- [try label] Interchange tag code [/try]
| Parameter
|
Description
|
Default
|
| label |
The label shared by the paired [try] and [catch] blocks |
default
|
| clean |
Suppress [try] block output if an error is detected. |
None |
| status |
Returns 0 (failed) or 1 (success) instead of the usual block output. |
None |
| hide |
Suppresses output from the code block. |
None |
| interpolate |
Parse Interchange tags, included in the body text, before this container tag executes. |
No
|
Examples
The [try] tag will run a block of Interchange tags and trap
any errors found.
[try my_label]
Body text to parse (and return if no error)
[/try]
|
The [catch] tag can then handle any errors that were trapped by the
[try] block.
[catch label=my_label other_named_attributes]
[/Pattern matching error message 1/]
Return this if error 1 occurs
[/Pattern matching error message 1/]
[/Pattern matching error message 2/]
Return this if error 2 occurs, etc.
[/Pattern matching error message 2/]
Default body text to process if try block caused an error
[else]
Optional body text to process if there was no error
[/else]
[/catch]
|
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 an error is detected, interchange will executes the corresponding
[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
The [catch] block must occur after the [try] block,
on the same page.
|
The try tag will also place a result in the $Session object.
For example, the following returns the "Illegal division by zero" error message:
[set divisor]0[/set]
[try label=div]
[perl] 1 / $Scratch->{divisor} [/perl]
[/try]
[catch div]
[/Illegal division by zero/]
Division by zero
[/Illegal division by zero/]
[/eval "string" trapped by operation mask/]
Perl Safe error
[/eval "string" trapped by operation mask/]
Other division error.
[/catch]
|
The $Session->{try}->{divide} object will be set to an empty
string if there was no error or,
if there was an error, it will contain the error message.
The [perl], [calc] and
[calcn] tags also set
$Session->{try}->{active_label} upon error.
Parameters
label
This is the label specifying the corresponding
[catch] block.
Defaults to "default".
clean
Setting clean=1 will cause the [try] block to suppress
its output only if an error is detected.
Otherwise, if clean is not set true,
the [try] block will return whatever partial output it has
completed before the error.
If an error is detected then the [catch] block will be
executed as usual.
status
Suppresses [try] block output, and instead returns either
1 if no error, or 0 if an error occurred.
If an error is detected then the [catch] block will be
executed as usual.
hide
Suppresses [try] block output (regardless of success or failure).
If an error is detected then the [catch] block will be
executed as usual.