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.
|
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
|
||
|
If a syntax error is deliberately introduced by omitting the second bracket at the end of line 1, leaving the "if" statement incomplete, ie:
Then this produces the compilation error:
Error when compiling J:\javascript\demo.js: at line
2:
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:
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 %%%
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. |