WeCodeAcademy

Handling Errors with try/catch

Code that might fail can be wrapped in try. If it throws, catch runs instead of the program crashing.

Examplejavascript
Open in editor »
Quick check

When does the `catch` block run?

Your turn: write the code

Write safeParse(text) that returns JSON.parse(text), or null if the text is not valid JSON. Print safeParse('{"a":1}').a and then safeParse("oops").

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.