Catch the parse failure and report a message.
Acceptance criteria
- 1.#out should show could not parse that.
JavaScript Tutorial · JavaScript Errors
These 10 tasks go with the JavaScript try catch chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “try runs code that might fail, and catch handles it when it does.” .
1.Handle bad JSONexercise · easy · 10 min
Catch the parse failure and report a message.
Passes when: #out should show could not parse that.
2.Parse valid JSONexercise · easy · 10 min · practice plan
Parse good JSON and report the name from it.
Passes when: #out should show Ada.
3.Declare the variable outsideexercise · easy · 10 min · practice plan
Keep the value usable after the try block.
Passes when: #out should show 42.
4.Catch without the parameterexercise · easy · 10 min · practice plan
Use a catch block that does not name the error.
Passes when: #out should show it failed.
5.Record why it failedexercise · medium · 10 min · practice plan
Report the error name rather than swallowing it.
Passes when: #out should show failed: TypeError.
6.Fix the variable declared insidebug fix · easy · 8 min
A variable declared in the try block cannot be read outside it.
Passes when: #out should show 42.
7.Fix the empty catchbug fix · easy · 8 min · practice plan
Swallowing the error silently hides what happened.
Passes when: #out should show failed: TypeError.
8.Fix the try block wrapped too widelybug fix · easy · 8 min · practice plan
Only the parse can fail, so only it belongs inside.
Passes when: #out should show Ada.
9.Fix the catch that reruns the failurebug fix · easy · 8 min · practice plan
Repeating the failing call inside catch throws again.
Passes when: #out should show handled once.
10.Fix the result read before assignmentbug fix · easy · 8 min · practice plan
The value is used before the try block has set it.
Passes when: #out should show parsed fine.
Between them these tasks cover try, catch and Scope.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.