BeginnerCoder

Lesson 11 — Error handling in Node

Errors must not stop the server

If an error is not handled, a Node process crashes and every user loses the site. try { ... } catch (e) { ... } runs risky code and, if it throws an error, jumps to the catch block instead of crashing. e.message is the error's text.

try { JSON.parse("oops"); } catch (e) { console.log("bad JSON"); }

Why it matters: one bad request should give one error message — not take the whole server down.

Want it another way?
Task

Wrap the parse in try/catch so bad input logs "Handled: bad input" and the loop continues.

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.