JavaScript Error Handling Patterns — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Error Handling Patterns lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Return a fallback
easy10 minParse the text and return null when it fails.
- 2
Fail fast on bad input
easy10 minCheck the types at the top and throw immediately.
- 3
Use a guard clause
easy10 minReturn a default rather than throwing for missing input.
- 4
Add context when rethrowing
easy10 minReplace the parse error with a clearer message.
- 5
Handle at the boundary
medium10 minLet the loop fail and catch once at the top.
Find the bug(5)
- 1
Fix the missing input check
easy8 minWith no check the text is coerced and a wrong answer is returned.
- 2
Fix the error caught and ignored
easy8 minCatching without acting hides a genuine bug.
- 3
Fix the rethrow with no new information
easy8 minRethrowing the same message adds nothing.
- 4
Fix the throw where a default would do
easy8 minA missing name is expected, so return a greeting instead.
- 5
Fix the handler inside the loop
easy8 minOne handler at the boundary is enough.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace