[Go to site: main page, start]

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

DOMException: DOMException() コンストラクター

Baseline 広く利用可能

この機能は広く実装されており、多くのバージョンの端末やブラウザーで動作します。2020年1月以降、すべてのブラウザーで利用可能です。

DOMException() コンストラクターは、指定されたメッセージと名前を持つ DOMException オブジェクトを返します。

構文

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

引数

message 省略可

この例外の説明です。存在しない場合は、空文字列 '' が使用されます。

name 省略可

文字列です。指定された名前が標準エラー名であった場合、その DOMException オブジェクトの code プロパティは指定された名前に対応するコード番号を返します。

返値

新しく作成された DOMException オブジェクトです。

この例では、ボタンを押すと独自の DOMException が発生し、それがキャッチされ、独自のエラーメッセージがアラートに表示されます。

HTML

html
<button>Trigger DOM Exception</button>

<p id="output"></p>

JavaScript

js
const button = document.querySelector("button");

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

仕様書

仕様書
Web IDL
# dom-domexception-domexception

ブラウザーの互換性

関連情報