Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Proper Way to Catch Exception from JSON Parse



The best way to catch invalid JSON parsing errors is to put the calls to JSON.parse() to a try/catch block.

Example

function parseJSONSafely(str) {
   try {
      return JSON.parse(str);
   }
   catch (e) {
      console.err(e);
      // Return a default object, or null based on use case.
      return {}
   }
}
Updated on: 2019-12-02T06:17:05+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements