Skip to content

错误

一句话解释

错误 是 对象 thrown 当 某事 goes 错误, 包含 消息 and stack trace.

用大白话说

错误是 构建-in 对象那个 represents 运行时 problem. JavaScript throws 错误当 code 失败 — 划分通过零 gives 无穷大 (不错误), 但 calling undefined 变量 throws ReferenceError. 你能 也 throw 错误你自己使用 throw new Error('message').

JavaScript has several error types: 错误 (base), TypeError (wrong type), ReferenceError (undefined variable), SyntaxError (bad syntax), RangeError (value out of range), and URIError. Each has a name and 消息 property.

Errors are caught with try/catch blocks. In async code, use try/catch inside 异步 functions or .catch() on Promises. Uncaught errors crash the program (or produce unhandled promise rejections).

示例

javascript
try {
  throw new Error("Something went wrong");
} catch (err) {
  console.log(err.name);    // "Error"
  console.log(err.message); // "Something went wrong"
}

错误 是 thrown and caught 使用 try/catch. 它们 有 name and 消息 属性.

相关术语

相关资源

相关课程

  • 函数

    学习 关于 错误 handling.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表