在JavaScript中你会发现各种错误对象
JavaScript给了我们8个错误对象,根据错误类型在try/catch表达式中被提出。
ErrorEvalErrorRangeErrorReferenceErrorSyntaxErrorTypeErrorURIError
让我们来分析一下其中的每一个对象。
Error
这是一般的错误,所有其他的错误对象都是继承自它。你永远不会直接看到Error 的实例,而是由JavaScript触发上面列出的其他错误之一,这些错误继承自Error 。
它包含两个属性。
message:错误描述,一个人类可读的信息,应该解释发生了什么错误name错误类型(假设特定的错误对象名称的值,例如:TypeError或SyntaxError)。
并只提供了一个方法,toString() ,负责从错误中生成一个有意义的字符串,可以用来将其打印到屏幕上。
EvalError
这个错误在现代JavaScript中被定义了,但实际上从来没有被JavaScript抛出过,仍然是为了兼容的目的。它在ECMAScript 3中被定义,但从ECMAScript 5.1开始就没有出现在标准中。
它被用来表示全局函数eval() 被错误地使用,与它的定义不相容。
RangeError
当一个数字值不在其允许的范围内时,RangeError 将被触发。
最简单的例子是当你把一个数组的长度设置为一个负值时。
[].length = -1 //RangeError: Invalid array length
或者当你把它设置为一个高于4294967295
[].length = 4294967295 //4294967295
[].length = 4294967296 //RangeError: Invalid array length
(这个神奇的数字在JavaScript规范中被指定为32位无符号整数的最大范围,相当于Math.pow(2, 32) - 1)
下面是你在野外可以发现的最常见的范围错误。
RangeError: argument is not a valid code pointRangeError: invalid array lengthRangeError: invalid dateRangeError: precision is out of rangeRangeError: radix must be an integerRangeError: repeat count must be less than infinityRangeError: repeat count must be non-negative
ReferenceError
ReferenceError 表示检测到了一个无效的参考值:一个JavaScript程序试图读取一个不存在的变量。
dog //ReferenceError: dog is not defined
dog = 2 //ReferenceError: dog is not defined
请注意,如果不是在严格模式下运行,上述语句将在全局对象上创建一个dog 。
下面是你在野外可以发现的最常见的引用错误。
ReferenceError: "x" is not definedReferenceError: assignment to undeclared variable "x"ReferenceError: can't access lexical declaration 'X' before initializationReferenceError: deprecated caller or arguments usageReferenceError: invalid assignment left-hand sideReferenceError: reference to undefined property "x"
SyntaxError
当在程序中发现语法错误时,会产生一个SyntaxError 。
下面是一些产生语法错误的代码例子。
一个没有名字的函数语句。
function() {
return 'Hi!'
}
//SyntaxError: function statement requires a name
在一个对象属性定义后缺少逗号。
const dog = {
name: 'Roger'
age: 5
}
//SyntaxError: missing } after property list
以下是你在野外可以发现的最常见的语法错误。
SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecatedSyntaxError: "use strict" not allowed in function with non-simple parametersSyntaxError: "x" is a reserved identifierSyntaxError: JSON.parse: bad parsingSyntaxError: Malformed formal parameterSyntaxError: Unexpected tokenSyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# insteadSyntaxError: a declaration in the head of a for-of loop can't have an initializerSyntaxError: applying the 'delete' operator to an unqualified name is deprecatedSyntaxError: for-in loop head declarations may not have initializersSyntaxError: function statement requires a nameSyntaxError: identifier starts immediately after numeric literalSyntaxError: illegal characterSyntaxError: invalid regular expression flag "x"SyntaxError: missing ) after argument listSyntaxError: missing ) after conditionSyntaxError: missing : after property idSyntaxError: missing ; before statementSyntaxError: missing = in const declarationSyntaxError: missing \] after element listSyntaxError: missing formal parameterSyntaxError: missing name after . operatorSyntaxError: missing variable nameSyntaxError: missing } after function bodySyntaxError: missing } after property listSyntaxError: redeclaration of formal parameter "x"SyntaxError: return not in functionSyntaxError: test for equality (==) mistyped as assignment (=)?SyntaxError: unterminated string literal
TypeError
当一个值的类型与预期的不同时,就会发生TypeError 。
最简单的例子是试图调用一个数字。
1() //TypeError: 1 is not a function
以下是你在野外可以发现的最常见的类型错误。
TypeError: "x" has no propertiesTypeError: "x" is (not) "y"TypeError: "x" is not a constructorTypeError: "x" is not a functionTypeError: "x" is not a non-null objectTypeError: "x" is read-onlyTypeError: 'x' is not iterableTypeError: More arguments neededTypeError: Reduce of empty array with no initial valueTypeError: can't access dead objectTypeError: can't access property "x" of "y"TypeError: can't define property "x": "obj" is not extensibleTypeError: can't delete non-configurable array elementTypeError: can't redefine non-configurable property "x"TypeError: cannot use 'in' operator to search for 'x' in 'y'TypeError: cyclic object valueTypeError: invalid 'instanceof' operand 'x'TypeError: invalid Array.prototype.sort argumentTypeError: invalid argumentsTypeError: invalid assignment to const "x"TypeError: property "x" is non-configurable and can't be deletedTypeError: setting getter-only property "x"TypeError: variable "x" redeclares argument
URIError
这个错误是在调用一个处理URI的全局函数时发生的。
decodeURI()decodeURIComponent()encodeURI()encodeURIComponent()
并传递一个无效的URI。