Skip to content

错误

学习什么错误是, difference 之间语法 and 运行时错误, and 如何 handle 它们 gracefully.

你将学到

  • 什么错误是 and 为什么 programs produce 它们
  • difference 之间语法, 运行时, and logic 错误
  • 如何 try/catch lets 你 handle 错误 gracefully
  • 如何 读取错误消息 and stack trace

概念讲解

Errors

An error is a signal that something went wrong while running your program. Errors are not failures of you as a programmer — they are normal, and learning to read and handle them is a core skill.

Three Kinds of Errors

  1. Syntax errors — the code is malformed and cannot be parsed at all. A missing bracket or stray comma. The program never runs.
  2. Runtime errors — the code is syntactically valid but fails during execution. Calling a function that does not exist, or reading a property of 空.
  3. Logic errors — the program runs without crashing but produces the wrong result. The hardest to find, because no error message is shown.

Reading Error Messages

A runtime error prints a message and a stack trace — the chain of function calls that led to the error. Read the trace from the top: the first line usually names the file and line number where the error occurred.

TypeError: Cannot read properties of undefined (reading 'name')
    at getUser (app.js:12)
    at render (app.js:25)

This tells you: at line 12 of app.js, inside getUser, the code tried to read .name from undefined.

try / catch

You can catch a runtime error and handle it gracefully instead of crashing:

try {
  const data = JSON.parse(brokenInput);
} catch (error) {
  console.log("无效 JSON:", error.message);
}

The code in try runs normally; if it throws, execution jumps to catch with the error object. The program continues instead of crashing.

The Mindset

Errors are feedback, not punishment. Each error message is a clue. When you see one, read it carefully — it usually tells you exactly what went wrong and where.

示例代码

              // A function that can fail:
function parseAge(input) {
  const age = Number(input);
  if (Number.isNaN(age)) {
    throw new Error(`${input} is not a valid number`);
  }
  if (age < 0) {
    throw new Error("Age cannot be negative");
  }
  return age;
}

// Handle errors gracefully with try/catch:
try {
  const result = parseAge("twenty");
  console.log("Age:", result);
} catch (error) {
  console.log("Could not parse age:", error.message);
  // "Could not parse age: twenty is not a valid number"
}
            

parseAge throws 自定义错误 for 无效输入. try/catch 围绕 call prevents program 从 crashing and shows friendly 消息相反.

动手试一试

  • JSON Validator

    粘贴 broken JSON and 读取错误消息. 这个 tool catches 语法错误 and shows 你精确地哪里它们是.

常见错误

错误写法

Ignoring 错误消息相反的 读取它们.

正确做法

读取消息 and stack trace. 它们通常 name file, 行, and problem. 30-其次读取能 保存 30 minutes 的 guessing.

错误写法

Using try/catch 到 silently swallow 错误使用空 catch 块.

正确做法

At 最小值, log 错误 in catch 块. Silent catches hide bugs and 制作调试远 harder later.

相关工具

练习 错误

4 个练习

练习 错误

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

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

查看错误