try catch in javascript not working code example
Example 1: js should i use try catch
One should avoid throw errors as the way to pass error conditions
around in applications.
The throw statement should only be used
"For this should never happen, crash and burn. Do not
recover elegantly in any way"
try catch however is used in situation where host objects
or ECMAScript may throw errors.
Example:
-------------------------------------
var json
try {
json = JSON.parse(input)
} catch (e) {
json = null
}
-------------------------------------
Example 2: js try..catch works synchronously.
Your question is confusingly worded.
The entire Javascript language is fully synchronous; all language constructs, including catch and finally blocks, will execute synchronously before running the next line of code.
However, they are not aware of any asynchronous operations that may have begun, and will not wait for them to finish.