Question

I am wrapping my code into try/catch blocks and I decided to test it out to see how it works. Below is a simple snippet of code that will generate a Syntax Error - trigge rHandler

try{
    $(document).trigge rHandler('fbload');
}catch(e){
     alert(e);
}

However I'm not getting the alert! Instead the error is logged in the console as an Unhandled Syntax Error. I was expecting that any error that is generated inside the Try block will automatically be passed down into the Catch section where I can do anything I want with it? Why does this not appear to be working?

Was it helpful?

Solution

try..catch will catch exceptions which occur at runtime. But Syntax errors occur during parsing time itself. So, when the code

$(document).trigge rHandler('fbload');

is encountered, JavaScript tries to parse the expression. But it couldn't. So it is clueless and fails immediately with SyntaxError and that is why it is not caught by the except block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top