Loading...

JavaScript Errors

An error is how a function refuses to continue. This page uses { drink: "tea" } with no size. throw new Error("missing size") stops label immediately. catch receives that Error. The page line reads caught / missing size.

Not the debugging page

The previous page called console.error("missing size") and then returned a fallback string. The script kept going. Nothing was thrown.
throw is different. The lines after it in that function do not run. Only catch can take over.

An Error carries two strings

new Error("missing size") creates an object. error.name is Error. error.message is the text you passed, missing size. Logging the object is less useful than logging those two fields.

catch receives the Error

label throws when size is missing. The call sits inside try, so catch runs and prints error.message.

Catch a missing size

javascript
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
try {
console.log(label({ drink: "tea" }));
} catch (error) {
console.log(error.message);
}

The log is missing size. label never reached its return, so tea / missing size is not produced here.

A good order skips catch

Give size the value regular. label returns normally. catch does not run.

A complete order

javascript
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
try {
console.log(label({ drink: "tea", size: "regular" }));
} catch (error) {
console.log(error.message);
}

The log is tea / regular.

finally runs either way

finally runs after try when the call succeeds, and after catch when it throws. Use it for a step that must happen in both cases.

finally after a throw

javascript
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
try {
label({ drink: "tea" });
} catch (error) {
console.log(error.message);
} finally {
console.log("done");
}

The logs are missing size and then done.

Read name and message

Catch the same Error and print the two fields on separate lines.

Name and message

javascript
try {
throw new Error("missing size");
} catch (error) {
console.log(error.name);
console.log(error.message);
}

The logs are Error and then missing size.

The checker throws, the caller catches

requireSize knows the rule. It throws and does not build a label. The caller decides what to print.

Separate the check

javascript
function requireSize(order) {
if (!order.size) {
throw new Error("missing size");
}
}
try {
requireSize({ drink: "tea" });
console.log("ok");
} catch (error) {
console.log(error.message);
}

The log is missing size. The line console.log("ok") does not run, because the throw jumped to catch.

The line under the cup

The page calls label without a size. catch builds the visible line from the message.

Page demo — a caught error

html
<!DOCTYPE html>
<html>
<body>
<p id="line">…</p>
<script>
function label(order) {
if (!order.size) {
throw new Error("missing size");
}
return order.drink + " / " + order.size;
}
let line = "";
try {
line = label({ drink: "tea" });
} catch (error) {
line = "caught / " + error.message;
}
document.getElementById("line").textContent = line;
</script>
</body>
</html>

The line reads caught / missing size. A complete order would have returned tea / regular and left catch unused. fixed / tea / regular was the debugging page.

At the counter

  1. Call label({ drink: "tea" }) inside try. The catch log is missing size.
  2. Call label({ drink: "tea", size: "regular" }). The log is tea / regular, and catch does not run.
  3. Add finally around the failing call. The second log is done.
  4. Read error.name and error.message. The logs are Error and missing size.
  5. Read the page line: caught / missing size.

Misses at the counter

throw stops the function. console.error does not.

  • Expecting the lines after throw to run
  • Forgetting catch and leaving the error uncaught
  • Thinking finally runs only after success
  • Reading error.message as the name Error
  • Reading the line as fixed / tea / regular

Keep this

  • throw new Error("missing size") stops label before return
  • catch receives the Error; error.message is missing size
  • A complete order logs tea / regular and skips catch
  • finally runs after success and after a throw
  • The page line reads caught / missing size

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top