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.
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.
A quick 3-question quiz on this lesson — instant feedback.
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.
Project-based JavaScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for JavaScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →