Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Window: reportError() method

ThereportError() method of theWindow interface may be used to report errors to the console or event handlers of global scopes, emulating an uncaught JavaScript exception.

This feature is primarily intended for custom event-dispatching or callback-manipulating libraries.Libraries can use this feature to catch errors in callback code and re-throw them to the top level handler.This ensures that an exception in one callback will not prevent others from being handled, while at the same time ensuring that stack trace information is still readily available for debugging at the top level.

Syntax

js
reportError(throwable)

Parameters

throwable

An error object such as aTypeError.

Return value

None (undefined).

Exceptions

TypeError

The method is called without an error argument.

Examples

Feature test for the method using:

js
if (typeof window.reportError === "function") {  // function is defined}

The following code shows how you might create and report an error, and how it may be caught using either theonerror event handler property or by adding a listener for theerror event.Note that the handler assigned toonerror must returntrue to stop the event propagating further.

js
const newError = new Error("Some error message", "someFile.js", 11);window.reportError(newError);window.onerror = (message, source, lineno, colno, error) => {  console.error(`message: ${error.message}, lineno: ${lineno}`);  return true;};window.addEventListener("error", (error) => {  console.error(error.filename);});// Output// > "message:Some error message, lineno: 11"// > "someFile.js"

Specifications

Specification
HTML
# runtime-script-errors

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp