I have a Javascript functions which throw exceptions if something goes wrong. I have so used to this behavior, that one day it took me more than 20 minutes to find the error in code because the function did not threw exception, it just failed silently.
Try the following code in Firefox with Firebug plug-in.
Create a html page with a function:
function myfunc(key)
{
alert('I started');
if(!key)
throw 'myfunc: Invalid arguments';
}
Then create a second html page with code:
<script type="text/javascript">
myfunc(null);
alert('Exception was before me!');
</script>
and load this second html page from the first page with jQuery $.ajax function.
The code will be executed correctly, but you’ll see no exceptions logged in the Firebug console.
If you run the same function myfunc(null) right on the frst html page, you’ll se iimediately a red line in Firebug saying:
uncaught exception: myfunc: Invalid arguments
But there is nothing logged out if it executed by jQuery (I guess, jQuery uses eval to run
scripts after $.ajax).
Does this mean that jQuery itself catches exceptions and ignores them while evaluating Javascript? Is there any way I can tell jQuery to let my exceptions to go through?
It seems, this was a bug or a missing feature of jQuery 1.4.
We upgraded to 1.6 and now exceptions are getting through even when the Javascript code is loaded and evaluated by jQuery $.ajax call.