RangeError

RangeError对象标明一个错误,当一个值不在其所允许的范围或者集合中。

语法

new RangeError([message[, fileName[, lineNumber]]])

参数

message
可选,可读的错误描述
fileName This API has not been standardized.
可选,包含造成异常代码的文件名
lineNumber This API has not been standardized.
可选,造成异常的代码所在的行数

描述

试图传递一个number参数给一个范围内不包含该number的函数时则会引发RangeError。当传递一个不合法的length值作为Array 构造器的参数创建数组,或者传递错误值到数值计算方法(Number.toExponential()Number.toFixed()Number.toPrecision()),会出现RangeError。.

属性

RangeError.prototype
允许在RangeError对象上附加属性。

方法

RangeError全局对象没有自带方法,但它通过可以原型链继承一些方法。

RangeError实例

属性

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError/prototype', 'Properties')}}

方法

{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError/prototype', 'Methods')}}

例子

使用RangeError

var check = function(num) {
  if (num < MIN || num > MAX) {
    throw new RangeError('Parameter must be between ' + MIN + ' and ' + MAX);
  }
};

try {
  check(500);
}
catch (e) {
  if (e instanceof RangeError) {
    // 处理越界错误
  }
}

规范

Specification Status Comment
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
RangeError
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
RangeError
Standard  
ECMAScript (ECMA-262)
RangeError
Living Standard  

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help! (en-US)
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

相关连接