D3PLOT 22.1

Dealing with Errors in Scripts

Dealing with Errors in Scripts

Script errors come in two forms:

Syntax errors Are mistakes of JavaScript grammar or spelling, resulting in error messages during compilation.

These are easy to detect and correct since the line number and offending syntax are both described by the compiler. The script needs to be edited to correct the problem and then recompiled. Sometimes several iterations of the compile/edit cycle are required to eliminate all errors from a script.
Run-time errors Are errors of context or logic in scripts that are syntactically correct, and thus have compiled, but which fail at some stage when being run.

A typical example of a run-time error is an attempt to divide a value by zero, yielding the illegal result infinity. More subtle errors involve passing an invalid value to a function, accessing an array subscript that is out of range, and so on.

The JavaScript API Reference Manual has been written in such a way that it handles "harmless" run-time errors by issuing a warning and continuing execution, but that more serious errors which could result in the wrong answers being generated issue an error message and terminate.

Here is an example script which demonstrates both types of error. This script lists all the shell elements attached to the first node in the model, and calls to the JavaScript API are hyperlinked to their relevant function definitions.

JavaScript
if(i = GetElemsAtNode (1, SHELL))
{
Print ("Number of shell elements on node " + GetLabel (NODE, 1) + " = " + i.nn + "\n");

for(j=0; j<i.nn; j++)
{
k = j + 1;
Print ("Shell #" + k + " = " + GetLabel (SHELL, i.list[j]) + "\n");
}
}
else
{
Print ("No shells at node " + GetLabel (NODE, 1) + "\n");
}

This initial script is syntactically correct, and on an example model writes the following to the controlling terminal:

Number of shell elements on node 1 = 4
Shell #1 = 31318414
Shell #2 = 31318415
Shell #3 = 31319004
Shell #4 = 31319006

If a syntax error is deliberately introduced by omitting the second bracket at the end of line 1, leaving the "if" statement incomplete, ie:

JavaScript
if(i = GetElemsAtNode (1, SHELL)

Then this produces the compilation error:

Error when compiling J:\javascript\demo.js: at line 2:
SyntaxError: missing ) after condition

Which is computer-speak for "you left out the closing bracket on that ' if ' statement".

If a run-time error is deliberately introduced by omitting the second argument ( SHELL ) to GetElementsAtNode (), making the first line:

JavaScript
if(i = GetElemsAtNode (1))

Then this is not picked up during compilation because the syntax is correct, but shows up when the script is run with the message:

%%% ERROR %%%
Fewer than 2 arguments supplied to Javascript function <get_elements_at_node>

And the script run terminates prematurely.

Because this example script has only one call to GetElementsAtNode () it is easy to identify and correct the problem, but in more complex scripts with many such calls it may be necessary to insert diagnostic Print () statements in order to track down a particular error.