I am using window.addEventListener('message', fun) to register a few event listeners. What will happen if there is one event listener throws an exception? Whether other event listeners run as usual or are impacted by this exception? Is there a way to catch all exceptions outside fun function?
1 Answer
Inside the fun event listener, if you think an exception may be thrown, you should catch it and handle it inside the function.
function fun(){ try{ //something that may throw an exception }catch(err){ console.log(err.message);//log the exception message //handle exception } } If you want to catch all unhandled thrown exceptions, you can use window.onerror().
window.onerror = function(e){ alert(e); } undefinedFunction();
funshould be solely responsible to handle that exception. Ideally, there should not be any other impact offunthrowing an exception.