DOMException: DOMException() コンストラクター
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
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.onclick = () => {
try {
throw new DOMException("Custom DOM Exception Triggered.");
} catch (error) {
document.querySelector("#output").textContent = `Error: ${error.message}`;
}
};
例
仕様書
Specification |
---|
Web IDL Standard # dom-domexception-domexception |
ブラウザーの互換性
BCD tables only load in the browser
関連情報
DOMException
コンストラクターのポリフィルがcore-js
で利用できます