JavaScript try catch — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript try catch 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
Handle bad JSON
easy10 minCatch the parse failure and report a message.
- 2
Parse valid JSON
easy10 minParse good JSON and report the name from it.
- 3
Declare the variable outside
easy10 minKeep the value usable after the try block.
- 4
Catch without the parameter
easy10 minUse a catch block that does not name the error.
- 5
Record why it failed
medium10 minReport the error name rather than swallowing it.
Find the bug(5)
- 1
Fix the variable declared inside
easy8 minA variable declared in the try block cannot be read outside it.
- 2
Fix the empty catch
easy8 minSwallowing the error silently hides what happened.
- 3
Fix the try block wrapped too widely
easy8 minOnly the parse can fail, so only it belongs inside.
- 4
Fix the catch that reruns the failure
easy8 minRepeating the failing call inside catch throws again.
- 5
Fix the result read before assignment
easy8 minThe value is used before the try block has set it.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace