error handling

<!DOCTYPE html> <html> <body> </body> <script> try{ for(a = 2,a < 2;a++){ console.log(a); } catch (err) { console.log("Syntax Error Was Occured"); } </script> </html> from above example there was syntax error on for loop then i try to catch the error and it doesn't work
5 Replies
Abrar
Abrar9mo ago
why that was happened
vince
vince9mo ago
let a = 2
ChooKing
ChooKing9mo ago
for(let a = 2; a < 2; a++){
console.log(a);
}
for(let a = 2; a < 2; a++){
console.log(a);
}
Jochem
Jochem9mo ago
try-catch doesn't catch syntax errors, because they occur before code execution and make code execution impossible. try-catch only catches exceptions, which are runtime errors
Abrar
Abrar9mo ago
Thanks for the explanation sir