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 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.onclick = () => {
try {
throw new DOMException("Custom DOM Exception Triggered.");
} catch (error) {
document.querySelector("#output").textContent = `Error: ${error.message}`;
}
};
例
仕様書
| Specification |
|---|
| Web IDL> # dom-domexception-domexception> |
ブラウザーの互換性
関連情報
DOMExceptionコンストラクターのポリフィルがcore-jsで利用できます