BeginnerCoder

Lesson 18 — Error handling

Catch errors instead of crashing

When code throws an error, everything stops — unless the error happens inside try { }. Then JavaScript jumps to catch (e) { }, where e.message is the error's text. finally { } runs at the end either way, which is good for clean-up.

try { risky(); } catch (e) { console.log(e.message); } finally { console.log("done"); }

Why it matters: one bad value should show a friendly message, not a blank page.

Want it another way?
Task

Wrap risky() in try/catch/finally: log "Handled: <message>" in catch and "cleanup" in finally.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with JavaScript Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.