Learn JavaScript
Error Handling
36.5
Lesson

The throw operator

The data stored in the myJson variable is syntactically correct, but it does not contain the property name.

In the example given below, JSON.parse does not create an error. But this does not show the error message in the catch {...} block.

In such cases, we can use the throw operator to show the error message in the catch {...} block.

In the example given below, we are using the throw operator to generate a SyntaxError because name is an essential property for the user object.