JavaScript错误对象

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 happened

    message :错误描述,一种人类可读的消息,应解释发生了什么错误

  • name: the type of error occurred (assumes the value of the specific error object name, for example, TypeError or SyntaxError)

    name :发生的错误的类型(假定特定错误对象名称的值,例如TypeErrorSyntaxError )

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:

以下是您可以在野外发现的最常见参考错误:

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:

以下是您在野外发现的最常见的语法错误:

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:

以下是您可以在野外发现的最常见的类型错误:

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。

翻译自: https://flaviocopes.com/javascript-errors/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值