Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

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:

Code Block
themeRDark
languagejavascript
linenumberstrue
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:

Code Block
themeRDark
languagejavascript
linenumberstrue
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?!
Div
classbox suggest

The JS API documentation indicates the likelihood of an error being thrown by a function by showing a possible return value of <error>

Div
classbox

Contents

Assimilate
Availability
Status
colourGreen
title3.1+

Requires:

  • Warzone 3.1 or above
Div
classbox
Contents

Jump to:

Contents
Table of Contents
exclude
maxLevel5

Div
classbox

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