Question · JavaScript and TypeScript course · Module 2: Functions, Arrays and Objects

Error Basics - try/catch Syntax

What is the correct way to handle an error that may occur during JSON parsing in JavaScript?

Answer options

  • if (JSON.parse(data) === error) { handleError(); }
  • JSON.parse(data).onError(function(e) { handleError(e); });
  • try { JSON.parse(data); } catch (error) { handleError(error); }
  • error.catch(JSON.parse(data));
Show the answer

Correct answer:

  • try { JSON.parse(data); } catch (error) { handleError(error); }