JavaScript gives us 8 error objects, which are raised in a try/catch expression depending on the error type:
JavaScript为我们提供了8个错误对象 ,它们会根据错误类型在try / catch表达式中引发:
Error
Error
EvalError
EvalError
RangeError
RangeError
ReferenceError
ReferenceError
SyntaxError
SyntaxError
TypeError
TypeError
URIError
URIError
Let’s analyze each one of those.
让我们分析其中的每一个。
Error
(Error
)
This is the generic error, and it’s the one all the other error objects inherit from. You will never see an instance of Error
directly, but rather JavaScript fires one of the other errors listed above, which inherit from Error
.
这是一般错误,它是所有其他错误对象继承的错误。 您将永远不会直接看到Error
的实例,而是JavaScript触发上面列出的其他错误之一,这些错误继承自Error
。
It contains 2 properties:
它包含2个属性:
message
: the error description, a human readable message that should explain what error happenedmessage
:错误描述,一种人类可读的消息,应解释发生了什么错误name
: the type of error occurred (assumes the value of the specific error object name, for example,TypeError
orSyntaxError
)name
:发生的错误的类型(假定特定错误对象名称的值,例如TypeError
或SyntaxError
)
and provides just one method, toString()
, which is responsible for generating a meaningful string from the error, which can be used to print it to screen.
并仅提供一种方法toString()
,该方法负责根据错误生成有意义的字符串,该字符串可用于将其打印到屏幕上。
EvalError
(EvalError
)
This error is defined in modern JavaScript but never actually thrown by JavaScript, and remains for compatibility purposes. It was defined in ECMAScript 3 but it’s not present in the standard since ECMAScript 5.1.
此错误是在现代JavaScript中定义的,但实际上从未由JavaScript抛出,并且出于兼容性目的而保留。 它是在ECMAScript 3中定义的,但是自ECMAScript 5.1起,它就不在标准中。
It was used to indicate that the global function eval()
was used incorrectly, in a way incompatible with its definition.
它用于指示全局函数eval()
使用不正确,其定义不兼容。
RangeError
(RangeError
)
A RangeError
will fire when a numeric value is not in its range of allowed values.
当数值不在其允许值范围内时,将触发RangeError
。
The simplest example is when you set an array length to a negative value:
最简单的示例是将数组长度设置为负值时:
[].length = -1 //RangeError: Invalid array length
or when you set it to a number higher than 4294967295
或将其设置为大于4294967295
的数字时
[].length = 4294967295 //4294967295
[].length = 4294967296 //RangeError: Invalid array length
(this magic number is specified in the JavaScript spec as the maximum range of a 32-bit unsigned integer, equivalent to Math.pow(2, 32) - 1
)
(此魔术数字在JavaScript规范中指定为32位无符号整数的最大范围,等效于Math.pow(2, 32) - 1
)
Here are the most common range errors you can spot in the wild:
以下是您可以在野外发现的最常见的量程误差:
ReferenceError
(ReferenceError
)
A ReferenceError
indicates that an invalid reference value has been detected: a JavaScript program is trying to read a variable that does not exist.
ReferenceError
指示已检测到无效的参考值:JavaScript程序正在尝试读取不存在的变量。
dog //ReferenceError: dog is not defined
dog = 2 //ReferenceError: dog is not defined
Be aware that the above statement will create a dog
variable on the global object if not ran in strict mode.
请注意,如果未在严格模式下运行,则以上语句将在全局对象上创建一个dog
变量。
Here are the most common reference errors you can spot in the wild:
以下是您可以在野外发现的最常见参考错误:
ReferenceError: can't access lexical declaration 'X' before initialization
ReferenceError: can't access lexical declaration 'X' before initialization
SyntaxError
(SyntaxError
)
A SyntaxError
is raised when a syntax error is found in a program.
在程序中发现语法错误时引发SyntaxError
。
Here are some examples of code that generate a syntax error.
以下是一些生成语法错误的代码示例。
A function statement without name:
没有名称的函数语句:
function() {
return 'Hi!'
}
//SyntaxError: function statement requires a name
Missing comma after an object property definition:
对象属性定义后缺少逗号:
const dog = {
name: 'Roger'
age: 5
}
//SyntaxError: missing } after property list
Here are the most common syntax errors you can spot in the wild:
以下是您在野外发现的最常见的语法错误:
SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
SyntaxError: "use strict" not allowed in function with non-simple parameters
SyntaxError: "use strict" not allowed in function with non-simple parameters
SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
SyntaxError: a declaration in the head of a for-of loop can't have an initializer
SyntaxError: a declaration in the head of a for-of loop can't have an initializer
SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
SyntaxError: for-in loop head declarations may not have initializers
SyntaxError: for-in loop head declarations may not have initializers
SyntaxError: identifier starts immediately after numeric literal
SyntaxError: identifier starts immediately after numeric literal
SyntaxError: test for equality (==) mistyped as assignment (=)?
SyntaxError: test for equality (==) mistyped as assignment (=)?
TypeError
(TypeError
)
A TypeError
happens when a value has a type that’s different than the one expected.
当值的类型不同于预期的类型时,将发生TypeError
。
The simplest example is trying to invoke a number:
最简单的示例是尝试调用数字:
1() //TypeError: 1 is not a function
Here are the most common type errors you can spot in the wild:
以下是您可以在野外发现的最常见的类型错误:
TypeError: can't define property "x": "obj" is not extensible
TypeError: can't define property "x": "obj" is not extensible
TypeError: cannot use 'in' operator to search for 'x' in 'y'
TypeError: cannot use 'in' operator to search for 'x' in 'y'
TypeError: property "x" is non-configurable and can't be deleted
TypeError: property "x" is non-configurable and can't be deleted
URIError
(URIError
)
This error is raised when calling one of the global functions that work with URIs:
调用与URI一起使用的全局函数之一时,会引发此错误:
decodeURI()
decodeURI()
decodeURIComponent()
decodeURIComponent()
encodeURI()
encodeURI()
encodeURIComponent()
encodeURIComponent()
and passing an invalid URI.
并传递无效的URI。