0

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
  • 1
    fun should be solely responsible to handle that exception. Ideally, there should not be any other impact of fun throwing an exception. Commented Jul 31, 2018 at 2:53

1 Answer 1

2

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();

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.