I guess this can be achieved using concepts from the Aspect-oriented programming.
We can simply create a wrapper of jQuery.event.dispatch
which will handle the errors:
(function () {
var temp = jQuery.event.handle;
jQuery.event.handle = function () {
try {
temp.apply(this, arguments);
} catch (e) {
console.log('Error while dispatching the event.');
}
}
}());
$(document).click(function () {
throw 'Some error...';
});
Using this approach you must be very careful because of changes in the internal interface
- Note the example above works for jQuery v1.6.3, in jQuery 1.7.1
jQuery.event.dispatch
instead of jQuery.event.handle
.