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.
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.
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.
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.
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.
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.
<!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
- Call
label({ drink: "tea" })insidetry. The catch log is missing size. - Call
label({ drink: "tea", size: "regular" }). The log is tea / regular, and catch does not run. - Add
finallyaround the failing call. The second log is done. - Read
error.nameanderror.message. The logs are Error and missing size. - Read the page line: caught / missing size.
Misses at the counter
throw stops the function. console.error does not.
- Expecting the lines after
throwto run - Forgetting
catchand leaving the error uncaught - Thinking
finallyruns only after success - Reading
error.messageas the nameError - Reading the line as fixed / tea / regular
Keep this
throw new Error("missing size")stopslabelbeforereturncatchreceives the Error;error.messageis missing size- A complete order logs tea / regular and skips
catch finallyruns after success and after a throw- The page line reads caught / missing size
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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