Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVladyslav Burdeniuk
16,795 PointsWhere should I place try-catch block of code
Where should I place try-catch block of code
var jsonString = 'This is not a JSON String';
var jsonObject = JSON.parse(jsonString);
1 Answer
Vladyslav Burdeniuk
16,795 PointsAjay, thank you) I have solved that issue by putting assignment in the try block as well
Ajay Prasannan
16,002 PointsAjay Prasannan
16,002 PointsHi Vladyslav,
The following worked for me:
As you can see, the potentially risky statement parsing the string into JSON is put within a try block, with a catch block following to handle any error.
In a real application, if the string value is only known at run-time, I guess that assignment could be moved into the try block as well. Can anyone else chime in on that? Both versions pass the code challenge.
Hope this helps,
Ajay