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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top