Skip to content

Application: error event ​

The error event fires when there is an uncaught exception. The runtime dispatches it itself; it doesn't come from the system.

Syntax ​

application.addEventListener('error', (event) => { })

Event object ​

ErrorEvent:

FieldTypeMeaning
messagestringA summary of the error, generated by the engine
errorunknownThe value that was thrown
filenamestringThe name of the script where the error occurred, such as 'main.js'
linenonumberThe line number where the error occurred

target is application. cancelable is true: calling preventDefault() stops the runtime from printing the default error log.

When it fires ​

  • When an exception occurs at the top level of main.js, in a timer callback, or in an event listener callback (including the sceneconnect callback and button tap callbacks) and isn't caught by try / catch. The script top level is verified; timer callbacks are not verified in the simulator.
  • When an event listener throws, error fires before the next listener for the same event runs, and the remaining listeners run as usual (verified by testing).
  • An exception thrown by an error listener itself doesn't fire error again; only the default log is printed to the console.
  • Promise rejections don't go here; they go to unhandledrejection.
  • When no listener calls preventDefault(), the runtime prints a log starting with Uncaught at the console.error level. Calling it cancels only this log: the exception has already happened, and the interrupted code doesn't resume.

Examples ​

js
application.addEventListener('error', (e) => {
  console.log('Error location', e.filename, e.lineno);
  console.log('Is an Error object', e.error instanceof Error);
  e.preventDefault();
});

throw new Error('Nobody catches this');

Result

  • The console prints Error location main.js followed by the line number of the throw line.
  • The console prints Is an Error object true.
  • Because preventDefault() was called, the runtime doesn't print the default error log.

Note: Put the listener at the very top of main.js. It doesn't receive exceptions thrown by top-level code that comes before it.

Corresponding iOS API ​

There's no corresponding system API. Corresponds to the browser's error event on window.

See also ​