Is there any way I could catch any uncaught exception in javascript? I mean, all my “dangerous” code are in try-catch blocks. But what about exceptions that I don’t handle explicitly? I’m using jQuery, my main javascript file starts with :
$(document).ready(function(){})
here I bind some events to some DOM elements. I can use try-catch blocks here, but they will catch exceptions that occur during the event binding procedure, not during the event handling. But if I used try-catch blocks in every event handling functions it would be ugly.
How should I catch exceptions that don’t occur in my explicit try-catch blocks? (I don’t want to write a general handler function, I just want to send the problem to my server)
You could write a function that would wrap your real handlers in a try/catch
(Might want to get fancier with argument handling etc.) Then when you bind handlers you’d do:
Also, if you want to make sure “
this” in the handler behaves as before, you can dofunc.apply($(this), e)instead of justfunc(e).