(info) Other great resources: Official JS API docs, Scripting Forum

Error Handling

Most JS API functions and Javascript operations will "thow" an error if you use them incorrectly...

 

Error objects

In Javascript, the Error object (or derivative) is used to provide details of an error. All error objects have two properties in common:

  • .name – the name (a text-based identifier) of the object
  • .message – a description of the error

Throwing errors

You can throw errors in your own code as follows:

throw new Error("your message");

If the error is not caught by the calling code it will ripple up to the Javascript engine running your script. When this happens a warning message will be shown in the console for the player under which the script is running (so the end-user might not see it unless they are in debug mode and have switched to the player associated with the script). In addition, errors are usually written to log files or the terminal.

Catching errors

Catch errors as follows:

try {
  // do something
} catch(e) {
  // this code will run if an error is thrown by something
  console.log(e.message);
} finally {
  // this block is optional
  // it will always be run regardless of whether an error is thrown or not
  // which seems pointless, as surely...
}
// ... you'd just run that code here instead?!
The JS API documentation indicates the likelihood of an error being thrown by a function by showing a possible return value of <error>. 
Availability 3.1+

Requires:

  • Warzone 3.1 or above
Contents

Jump to:

See also

Related articles:

  • hackAssert() – throw an error and trigger game assert if a value is falsey
  • out() – supports errors as an output method
  • Test API – unit test framework for WZ scripts

Â