Exceptions in asynchronous code in JavaScript
An exception thrown inside asynchronous
code cannot be caught via try-catch
:
try {
setTimeout(function() {
throw(new Error); // an exception will not be caught
}, 3000);
} catch(error) {
console.log(error);
}
Tell me what is the problem of the following code:
try {
elem.addEventListener('click', function() {
JSON.parse('some string');
});
} catch() {
console.log('invalid json');
}