RangeError

由於本文件沒有此語言的翻譯版本,您閱讀的是英文版的內容。 幫助我們翻譯這篇文章!

RangeError物件在一個給定的值不在允許的集合或範圍內時被作為一個錯誤拋出

語法

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

參數

message
可選。具人類可讀性的錯誤說明
fileName
可選。包含造成錯誤發生的程式碼的檔案名稱
lineNumber
可選。造成錯誤發生的程式碼行號

說明

當試著往一個 function 傳入一個不被其允許的值作為參數時,一個RangeError被拋出。這可在多種情況遭遇到,例如傳入一個不被允許的字串值到 String.prototype.normalize(),或試著透過 Array constructor 用一個不合法的長度來創建一個陣列,或往數值方法像是Number.toExponential()Number.toFixed()Number.toPrecision() 傳進糟糕的值。

屬性

RangeError.prototype
允許對一個 RangeError 物件增加其屬性。

方法

普遍的 RangeError 自身沒有包含方法,儘管他的確從原型鍊中繼承了一些。

RangeError 物件實體

屬性

RangeError.prototype.constructor
Specifies the function that created an instance's prototype.
RangeError.prototype.message
Error message. Although ECMA-262 specifies that RangeError should provide its own message property, in SpiderMonkey, it inherits Error.prototype.message.
RangeError.prototype.name
Error name. Inherited from Error.
RangeError.prototype.fileName
Path to file that raised this error. Inherited from Error.
RangeError.prototype.lineNumber
Line number in file that raised this error. Inherited from Error.
RangeError.prototype.columnNumber
Column number in line that raised this error. Inherited from Error.
RangeError.prototype.stack
Stack trace. Inherited from Error.

方法

Although the RangeError prototype object does not contain any methods of its own, RangeError instances do inherit some methods through the prototype chain.

範例

使用 RangeError (數值)

function check(n)
{
    if(!(n >= -500 && n <= 500))
    {
        throw new RangeError("The argument must be between -500 and 500.");
    }
}

try
{
    check(2000);
}
catch(error)
{
    if(error instanceof RangeError)
    {
        // Handle the error.
    }
}

使用 RangeError (非數值)

function check(value)
{
    if(["apple", "banana", "carrot"].includes(value) === false)
    {
        throw new RangeError("The argument must be an \"apple\", \"banana\", or \"carrot\".");
    }
}

try
{
    check("cabbage");
}
catch(error)
{
    if(error instanceof RangeError)
    {
        // Handle the error.
    }
}

規範

規範 狀態 注解
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition.
ECMAScript 5.1 (ECMA-262)
The definition of 'RangeError' in that specification.
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'RangeError' in that specification.
Standard  
ECMAScript Latest Draft (ECMA-262)
The definition of 'RangeError' in that specification.
Draft  

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
RangeErrorChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes

Legend

Full support  
Full support

另見