Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript try catch with json parse

I have several json objects that come from the server. At the moment, I'm using the browsers' json functionalities (my application only supports modern browsers) to parse json to objects.

Should I be using a try/catch to make my app more robust or would using try/catch create another set of problems?

like image 769
frenchie Avatar asked Mar 25 '26 07:03

frenchie


1 Answers

try..catch is not a magical construct to make problems go away. The question is, what would you put in your catch { } clause?

If you can do something useful if the json is broken. Something that allows you to make sure that the state of the app is correct again, then it makes sense.

But, what are the chances you will receive broken json? If you're generating and parsing the json yourself, the chances are low.

like image 165
Evert Avatar answered Mar 27 '26 19:03

Evert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!