pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/RangeError

href="?u=http://developer.mozilla.org/static/client/inter-latin.9a3b1bc220d426ef.woff2" as="font" type="font/woff2" crossorigen="anonymous" fetchpriority="low" />

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

RangeError

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

* Some parts of this feature may have varying levels of support.

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

語法

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

參數

message

可選。具人類可讀性的錯誤說明

fileName

可選。包含造成錯誤發生的程式碼的檔案名稱

lineNumber

可選。造成錯誤發生的程式碼行號

說明

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

屬性

RangeError

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

方法

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

RangeError 物件實體

屬性

RangeError.prototype.message

Error message. Inherited from Error.

RangeError.prototype.name

Error name. Inherited from Error.

RangeError.prototype.cause

Error cause. 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.

範例

使用 RangeError(數值)

js
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(非數值)

js
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.
  }
}

規範

Specification
ECMAScript® 2027 Language Specification
# sec-native-error-types-used-in-this-standard-rangeerror

瀏覽器相容性

參見

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy