Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zh-cn]: update DOMException constructor #16628

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions files/zh-cn/web/api/domexception/domexception/index.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
---
title: DOMException()
title: DOMException():DOMException() 构造函数
slug: Web/API/DOMException/DOMException
l10n:
sourceCommit: 41a8b9c9832359d445d136b6d7a8a28737badc6b
---

{{ APIRef("DOM") }}

**`DOMException()`** 构造函数返回一个包含指定的信息和名称的 `DOMException` 对象。
**`DOMException()`** 构造函数返回一个包含指定消息和名称的 {{domxref("DOMException")}} 对象。

## 语法

```
var domException = new DOMException();
var domException = new DOMException(message);
var domException = new DOMException(message, name);
```js-nolint
new DOMException()
new DOMException(message)
new DOMException(message, name)
```

### 参数

- `message` {{optional_inline}}
- : 对异常的描述。如果不存在,使用空字符串 `''` .
- : 对异常的描述。如果不存在,使用空字符串 `''`
- `name` {{optional_inline}}
- : 返回一个 {{domxref("DOMString")}} 包含与 [error constant](/zh-CN/docs/Web/API/DOMException#Error_constants) 错误相关的字符串常数之一
- : 一个字符串。如果指定的名称是[标准的错误名称](/zh-CN/docs/Web/API/DOMException#错误名称)之一,那么获取 `DOMException` 对象的 [`code`](/zh-CN/docs/Web/API/DOMException/code) 属性将返回与指定名称对应的代码编号

### 返回值

- **{{domxref("DOMException")}}**
- : 一个新创建的 {{domxref("DOMException")}} 对象。
一个新创建的 {{domxref("DOMException")}} 对象。

## 示例

在本例中,按下按钮会导致抛出自定义的 `DOMException` 异常,然后会捕获该异常并在警报中显示自定义的错误信息。

### HTML

```html
<button>触发 DOM 异常</button>

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

## 例子
### JavaScript

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

button.onclick = () => {
try {
throw new DOMException("已触发自定义 DOM 异常。");
} catch (error) {
document.querySelector("#output").textContent = `错误信息:${error.message}`;
jasonren0403 marked this conversation as resolved.
Show resolved Hide resolved
}
};
```

### 结果

{{ EmbedLiveSample('示例', '100%', 100) }}

## 规范

Expand All @@ -38,3 +65,7 @@ TBD
## 浏览器兼容性

{{Compat}}

## 参见

- [`core-js`](https://github.com/zloirock/core-js) 中一个可用的 [`DOMException` 构造函数的 polyfill](https://github.com/zloirock/core-js#domexception)
Loading