Name

try — safely execute a code block and test for errors

ATTRIBUTES

Attribute Pos. Req. Default Description
label 1 1 default Name to assign to the [try] block. The name is later used by cache (or some custom code) to refer to the proper [try] block.
status 0 0 0 Suppresses normal [try] block output and only return 1 for no error, or 0 when the error happens. The corresponding [catch] block is executed if there's an error.
hide 0 0 0 Suppresses normal [try] block output, regardless of its evaluation success or failure. The corresponding [catch] block is executed if there's an error.
clean 0 0 0 Cause the [try] block to suppress its output only if it has an error. Otherwise the block will return whatever partial output it has completed before the error. The corresponding [catch] block is executed if there's an error.
interpolate     0 interpolate input?
reparse     1 interpolate output?
hide     0 Hide the tag return value?

DESCRIPTION

The [try] block allows you to trap execution errors. Interchange processes the body of the tag and normally parses and evaluates the block. If no errors are raised during execution of the block, the parsing procedure continues as if [try] wasn't there. If the error does get generated, however, Interchange will execute the correspondingly named [catch] block. "Corresponding names" are determined by using labels — arbitrary strings that must match at both sides.

The [try] tag will place execution result in the $Session object. See the section called “EXAMPLES” for clarification.

BEHAVIOR

This tag does not appear to be affected by, or affect, the rest of Interchange.

EXAMPLES

Example: Simple 'try' block in action

[set divisor]0[/set]

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

[catch div]Division error[/catch]

Example: Triggering an illegal division by zero and watching the error message

As we've mentioned above, a [try] block labeled divide creates the $Session->{try}{divide} entry in Perl data structures:

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

[catch divide]
  Verbatim error message is: [calc]$Session->{try}{divide}[/calc]
[/catch]

NOTES

See [catch] for more examples and further discussion.

AVAILABILITY

try is available in Interchange versions:

4.6.0-5.7.0 (git-head)

SOURCE

Interchange 5.7.0:

Source: code/SystemTag/try.coretag
Lines: 15


# Copyright 2002-2007 Interchange Development Group and others
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.  See the LICENSE file for details.
# 
# $Id: try.coretag,v 1.4 2007-03-30 23:40:49 pajamian Exp $

UserTag try                 Order        label
UserTag try                 addAttr
UserTag try                 hasEndTag
UserTag try                 PosNumber    1
UserTag try                 Version      $Revision: 1.4 $
UserTag try                 MapRoutine   Vend::Interpolate::try

Source: lib/Vend/Interpolate.pm
Lines: 773

sub try {
my ($label, $opt, $body) = @_;
$label = 'default' unless $label;
$Vend::Session->{try}{$label} = '';
my $out;
my $save;
$save = delete $SIG{__DIE__} if defined $SIG{__DIE__};
$Vend::Try = $label;
eval {
  $out = interpolate_html($body);
};
undef $Vend::Try;
$SIG{__DIE__} = $save if defined $save;
if($@) {
  $Vend::Session->{try}{$label} .= "\n" 
    if $Vend::Session->{try}{$label};
  $Vend::Session->{try}{$label} .= $@;
}
if ($opt->{status}) {
  return ($Vend::Session->{try}{$label}) ? 0 : 1;
}
elsif ($opt->{hide}) {
  return '';
}
elsif ($opt->{clean}) {
  return ($Vend::Session->{try}{$label}) ? '' : $out;
}

return $out;
}

AUTHORS

Interchange Development Group

SEE ALSO

catch(7ic)

DocBook! Interchange!