try command-or-block catch [[local|my] $variable] command-or-block
Execute the command-or-block following the
try
keyword. If an error or exception
occurs during the evaluation, execute the
catch
command-or-block. If the
catch
keyword is followed by a
variable (possibly localized for the following block using
my or local)
and the try
block fails
with an exception, the error message of
the exception is stored to the variable before the
catch
block is executed.
The throw command as well as an equivalent
Perl construction perl { die "error message" }
allow user to throw custom exceptions.
Unless exception is raised or error occurs, this command
returns the return value of the try
block;
otherwise it returns the return value of
the catch
block.
Example 54. Handle parse errors
try { $doc:=open --format xml $input; } catch { try { echo "XML parser failed, trying HTML"; $doc := open --format html $input; } catch my $error { echo "Stopping due to errors: $error"; exit 1; } }