Error Handling with Try/Catch
Errors happen. Programs can trip over missing values or bad math.
try and catch let you handle a problem without crashing the whole show.
Think of a safety net under a trampoline.
try
catch
finally
throw
Reading the error
try
Put risky code inside try.
JavaScript will attempt it.
catch
If try fails, catch runs.
The error object can tell you what happened.
finally
finally runs after try and catch.
Use it for cleanup that should always happen.
throw
Use throw to make your own error on purpose.
That is useful when a value is not allowed.
Why this helps kids of all ages
Without catch, one bad line can stop everything.
With catch, you can show a friendly message and keep going.
Tip: Catch real problems. Do not hide every error forever. Fix the cause when you can.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.