DOMException

 DOMException 接口代表调用方法或访问 Web API 属性时发生的异常事件(被称为异常exception)。 这基本上是在 Web API 中如何描述错误情况的

每个异常都有一个名称 name,一个采用骆驼式命名法的简短字符串,用于描述识别错误或异常情况。

译者注:骆驼式命名法(CamelCase style),又称驼峰命名法。与 JavaScript 中更常见的小驼峰命名法有别,此处应是指大驼峰命名法。大驼峰命名法又称 Pascal 命名法。具体为名称中的每个单词仅首字母大写Capitalize),可参考下方错误名称。

构造函数

DOMException()
返回一个包含指定消息和名称的 DOMException 对象。

属性

DOMException.code 只读
返回一个 short,包含 error code constants 中的一个,或者返回 0,如果没有匹配的话。这个字段由于历史原因被使用。现在不再使用这个新的DOM异常:他们把这个信息放入 DOMException.name 属性。
DOMException.message 只读
返回一个 DOMString 代表与给定的错误名称有关信息或描述。
DOMException.name 只读
返回一个 DOMString 包含与错误名称相关的字符串.

错误名称

常见的错误名称列在这里。一些 API 定义了它们自己的名称组,所以这未必是一个完整的列表。

注意:因为很久以前错误是由数值(code value,代码值)与一个对应的命名变量定义的,所以以下部分条目包含过去使用的遗留代码值和常量名。

IndexSizeError
索引不在允许的范围内。例如,这可以被 Range 对象抛出。(遗留代码值:1,遗留常数名称:INDEX_SIZE_ERR)
HierarchyRequestError
节点树层次结构有误。(遗留代码值: 3 ,遗留常数名称:HIERARCHY_REQUEST_ERR)
WrongDocumentError
对象在错误的 Document中。(遗留代码值: 4,遗留常数名称: WRONG_DOCUMENT_ERR
InvalidCharacterError
字符串包含无效字符。(Legacy code value: 5 and legacy constant name: INVALID_CHARACTER_ERR
NoModificationAllowedError
对象不能被修改。(Legacy code value: 7 and legacy constant name: NO_MODIFICATION_ALLOWED_ERR
NotFoundError
找不到对象。(Legacy code value: 8 and legacy constant name: NOT_FOUND_ERR
NotSupportedError
不支持的操作。 (Legacy code value: 9 and legacy constant name: NOT_SUPPORTED_ERR)
InvalidStateError
对象是一个无效的状态. (Legacy code value: 11 and legacy constant name: INVALID_STATE_ERR)
SyntaxError
字符串不匹配预期的模式. (Legacy code value: 12 and legacy constant name: SYNTAX_ERR)
InvalidModificationError
对象不能被这种方式修改。 (Legacy code value: 13 and legacy constant name: INVALID_MODIFICATION_ERR)
NamespaceError
操作在XML名称空间是不允许的. (Legacy code value: 14 and legacy constant name: NAMESPACE_ERR)
InvalidAccessError
对象不支持此操作或参数 (Legacy code value: 15 and legacy constant name: INVALID_ACCESS_ERR)
TypeMismatchError
对象的类型不匹配预期的类型. (Legacy code value: 17 and legacy constant name: TYPE_MISMATCH_ERR) 这个值已被弃用,  JavaScript TypeError 异常被提出而不是DOMException
SecurityError
操作不安全。 (Legacy code value: 18 and legacy constant name: SECURITY_ERR)
NetworkError
网络错误发生. (Legacy code value: 19 and legacy constant name: NETWORK_ERR)
AbortError
T操作中止. (Legacy code value: 20 and legacy constant name: ABORT_ERR)
URLMismatchError
给定的URL不匹配另一个URL。 (Legacy code value: 21 and legacy constant name: URL_MISMATCH_ERR)
QuotaExceededError
给定配额已经超过了(Legacy code value: 22 and legacy constant name: QUOTA_EXCEEDED_ERR)
TimeoutError
操作超时. (Legacy code value: 23 and legacy constant name: TIMEOUT_ERR)
InvalidNodeTypeError
这个操作的节点是不正确的或祖先是不正确的. (Legacy code value: 24 and legacy constant name: INVALID_NODE_TYPE_ERR)
sDataCloneError
对象不可被克隆。 (Legacy code value: 25 and legacy constant name: DATA_CLONE_ERR)
EncodingError
编码或解码操作失败 (没有遗留代码值和常量的名字).
NotReadableError
输入/输出读操作失败(没有遗留代码值和常量的名字).
UnknownError
因未知的瞬态的原因使操作失败(例如 内存不足) (No legacy code value and constant name).
ConstraintError
条件没满足而导致事件失败的异常操作 (No legacy code value and constant name).
DataError
提供的数据不足 (No legacy code value and constant name).
TransactionInactiveError
请求被当前不活跃的事件或已完成事件阻止 (No legacy code value and constant name)。
ReadOnlyError
尝试操作 "readonly" 事件 (No legacy code value and constant name)。
VersionError
尝试打开一个比现有版本更低的数据库(No legacy code value and constant name)。
OperationError
因特定操作失败原因而失败 (No legacy code value and constant name).
NotAllowedError
<dd请求不被用户代理或当前上下文所在平台允许,可能因为用户拒绝授权 (no="" and="" code="" constant="" dd="" legacy="" name)。<="" value=""> </dd请求不被用户代理或当前上下文所在平台允许,可能因为用户拒绝授权>

规范

规范 状态 备注
Web IDL
constructor
Candidate Recommendation Adds the constructor for the DOMException class. Adds the NotReadableError, UnknownError, ConstraintError, DataError, TransactionInactiveError, ReadOnlyError, VersionError, OperationError, and NotAllowedError values.
DOM4
DOMException
Obsolete Added SECURITY_ERR, NETWORK_ERR, ABORT_ERR, URL_MISMATCH_ERR, QUOTA_EXCEEDED_ERR, TIMEOUT_ERR, INVALID_NODE_TYPE_ERR, and DATA_CLONE_ERR. The property code has been deprecated for exception values. The EncodingError value added.
Document Object Model (DOM) Level 3 Core Specification
DOMException
Obsolete Added of VALIDATION_ERR and TYPE_MISMATCH_ERR.
Document Object Model (DOM) Level 2 Core Specification
DOMException
Obsolete Added of INVALID_STATE_ERR, SYNTAX_ERR, INVALID_MODIFICATION_ERR, NAMESPACE_ERR, and INVALID_ACCESS_ERR.
Document Object Model (DOM) Level 1 Specification
DOMException
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
DOMExceptionChrome Full support YesEdge Full support YesFirefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
DOMException() constructorChrome Full support YesEdge No support NoFirefox ? IE ? Opera Full support YesSafari ? WebView Android Full support YesChrome Android Full support YesFirefox Android ? Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
codeChrome Full support YesEdge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
messageChrome Full support YesEdge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
nameChrome Full support YesEdge Full support 12Firefox Full support 1IE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

参见