I have a scenario where I need to execute code that may result in an error without catching it.
If the response is not valid JSON, then the desired outcome should be 0:
Here is the code snippet I am considering:
var overallProgress = try {JSON.parse(text)} ? 0;
Is there a way to achieve this?
Alternatively, could something like this work:
var overallProgress = try { JSON.parse(text)} catch(error) { 0 };
var overallProgress = try { JSON.parse(text)} catch(error) { overallProgress = 0 };
However, errors are encountered when using the above code.
I came across a similar post below but did not find any solution:
Is try {} without catch {} possible in JavaScript?
To make it easier on the eyes:
var overallProgress = 0;
try { overallProgress = JSON.parse(text).overallProgress }
catch(error) { overallProgress = 0 };