Expand/Shrink

Try/Catch Statement

A try statement allows a program to regain control following a normally fatal runtime error.
    try 
       <block> 
    catch e 
       <block> 
    end try 
In addition the builtin routine throw() can be used to transfer control directly to the catch block, potentially from within several nested routine calls.

The catch statement traps an exception within the try block, or any routines it invokes, that is not first caught by another nested try/catch statement.
Any exceptions that must be re-thrown must be done so explicitly, and unlike other languages there is only one (non-optional) catch clause.
There are no confusing classes of exceptions, and no implicit filtering, and hence no unexpected leaks to the outside world.

The exception variable is declared immediately after the catch keyword and is a sequence.
It can be given any valid identifier as a name, though "e" is usually sufficient.
For details of the contents of the exception variable, see throw().
Just like the loop control variables of for statements, the exception variable can be predeclared and then persists after the end try, otherwise it is automatically declared and drops out of scope on the end try.

pwa/p2js: Supported, however note that JavaScript does not (for instance) throw typecheck errors. While it is perfectly acceptable to use try/catch to aid debugging on desktop/Phix first, or handle rare and unusual cases with an explicit throw(), any program which relies on a specific exception (especially implicit) triggering for successful operation is fairly unlikely to work consistently under pwa/p2js.

See Also: throw
Expand/Shrink