Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. DOMException
  4. DOMException()

DOMException: DOMException() constructor

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨January 2020⁩.

Note: This feature is available inWeb Workers.

TheDOMException() constructor returns aDOMException object with a specified message and name.

Syntax

js
new DOMException()new DOMException(message)new DOMException(message, name)

Parameters

messageOptional

A description of the exception. If not present, the empty string'' is used.

nameOptional

A string. If the specified name is astandard error name, then getting thecode property of theDOMException object will return the code number corresponding to the specified name. If not present, the string'Error' is used.

Return value

A newly createdDOMException object.

Examples

In this example, pressing the button causes a customDOMException to be thrown, which is then caught and the custom error message shown in an alert.

HTML

html
<button>Trigger DOM Exception</button><p></p>

JavaScript

js
const button = document.querySelector("button");button.onclick = () => {  try {    throw new DOMException("Custom DOM Exception Triggered.");  } catch (error) {    document.querySelector("#output").textContent = `Error: ${error.message}`;  }};

Result

Specifications

Specification
Web IDL
# dom-domexception-domexception

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp