JavaScript global event mechanism

前端 未结 10 2085
无人及你
无人及你 2020-11-22 14:39

I would like to catch every undefined function error thrown. Is there a global error handling facility in JavaScript? The use case is catching function calls from flash that

10条回答
  •  心在旅途
    2020-11-22 15:11

    sophisticated error handling

    If your error handling is very sophisticated and therefore might throw an error itself, it is useful to add a flag indicating if you are already in "errorHandling-Mode". Like so:

    var appIsHandlingError = false;
    
    window.onerror = function() {
        if (!appIsHandlingError) {
            appIsHandlingError = true;
            handleError();
        }
    };
    
    function handleError() {
        // graceful error handling
        // if successful: appIsHandlingError = false;
    }
    

    Otherwise you could find yourself in an infinite loop.

提交回复
热议问题