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.
For example, you can use Try and Throw to escape from deep inside For loops.
You can also use Try and Throw to catch an exception that JMP itself throws:
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:
JSL also has a function called Function to extend the macro concept with a local context arguments. Suppose that you want to create a function that takes the square root but tolerates negative arguments, returning zero rather than errors. You first specify the local arguments in a list with braces { } and then state the expression directly. You do not need to enclose the expression in Expr because Function stores it as an expression implicitly.
In defined functions, the stored function is not accessible directly, even by the Name Expr command. If you need to access the function expression in your script, you have to create the function within an expr() clause. For example,
The use of Default Local localizes all the names that:
In both cases, the variable temp is not a global, or, if it is already a global, remains untouched by evaluating the functions.
Using Default Local in user-defined functions can cause some confusion because it is context-sensitive. That is, the same function may behave differently in different contexts, depending on whether same-named outer variables are in scope. The user should enumerate each and every variable they wish to be local. This reduces the confusion and the potential incorrect use of outer scope variable values.
The Recurse function makes a recursive call of the defining function. For example, you can make a function to calculate factorials. A factorial is the product of a number, the number minus 1, the number minus 2, and so on, down to 1.
You can define recursive calculations without using Recurse. For example, you could replace Recurse by myfactorial, and the script would still work. However, Recurse offers these advantages:
The Include function opens a script file, parses the script in it, and executes the JSL in the specified file.
See Advanced Scoping and Namespaces for information about using namespaces with your scripts.
Files with the .txt extension are treated as a JSL file. A text file that contains data can be included, however an error will appear since this is not valid JSL.
The Load Text File and Save Text File commands allow manipulation of text files from JSL. Note that the paths in the following code are strings.