try

Usage

try command-block catch [[local] $id] command-block

Description

Execute command-block following the try keyword. If an error or exception occures during the evaluation, execute the catch command-block. If a variable follows catch and the try block fails, an error message of the exception occured is stored to the variable before the catch block is executed. Optionally, the variable name may be preceded with the keyword local in order to make the assignment local to the catch block (see local).

The throw command and the equivalent perl construction perl { die "error message" } allow user to throw custom exceptions.

Example 31. Handle parse errors

try {
  open XML doc=$input;
} catch {
  try {
    echo "XML parser failed, trying HTML";
    open HTML doc=$input;
  } catch local $error {
    echo "Stopping due to errors: $error";
    exit 1;
  }
}

See Also

throw