A script can stop itself by executing the Throw() function. If you want to escape from part of a script when it is in an error condition, you can enclose it in a Try() expression.
Try() takes two expression arguments. It starts by evaluating the first expression, and if or when the first expression throws an exception by evaluating Throw, it does the following:
Throw() does not require an argument but has two types of optional arguments. If you include a character-valued expression as an argument, throwing stores that string in a global named exception_msg; this is illustrated in the first example below.
The following example prints a message to the log when the Try() expression cannot be executed:
Try(
If the Throw() string begins with “!” and is inside a Try() expression, throwing creates an error message about where the exception was caught.
Try(
Print( "This AFTER message will NOT print due to error." );
nr = N Row( a );
nc = N Col( a );
Try(
You do not have to use Try() to make use of Throw(). In this example, Throw() is not caught by Try() but still stops a script that cannot proceed: