/
index.md
74 lines (50 loc) · 1.76 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
title: "DOMException: DOMException() constructor"
short-title: DOMException()
slug: Web/API/DOMException/DOMException
page-type: web-api-constructor
browser-compat: api.DOMException.DOMException
---
{{ APIRef("DOM") }}
The **`DOMException()`** constructor returns a
{{domxref("DOMException")}} object with a specified message and name.
## Syntax
```js-nolint
new DOMException()
new DOMException(message)
new DOMException(message, name)
```
### Parameters
- `message` {{optional_inline}}
- : A description of the exception. If not present, the empty string `''` is
used.
- `name` {{optional_inline}}
- : A string. If the specified name is a [standard error name](/en-US/docs/Web/API/DOMException#error_names), then getting the [`code`](/en-US/docs/Web/API/DOMException/code) property of the `DOMException` object will return the code number corresponding to the specified name.
### Return value
A newly created {{domxref("DOMException")}} object.
## Examples
In this example, pressing the button causes a custom `DOMException` to be thrown, which is then caught and the custom error message shown in an alert.
### 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}`;
}
};
```
### Result
{{ EmbedLiveSample('Examples', '100%', 100) }}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [A polyfill of `DOMException` constructor](https://github.com/zloirock/core-js#domexception) is available in [`core-js`](https://github.com/zloirock/core-js)