troubleshooting Question

Throwing JS errors inside event listeners - FireFox

Avatar of ratWonder
ratWonder asked on
Web Languages and StandardsJavaScriptWeb Browsers
10 Comments1 Solution458 ViewsLast Modified:
Hiya guys.
I'm trying to throw errors in on of my functions. I am using this syntax:

---
var myFunction = function() {throw new Error('a new error')}
myFunction();
---

This works in both FireFox and IE. In FireFox JS console if gives you a nice pretty error with the line number and a reference to where the error occurred in the document.

However, if you try to throw this error from inside an event listener:

---
var myFunction = function() {throw new Error('a new error')}
if(window.addEventListener) window.addEventListener('load',myFunction,false);
else if(window.attachEvent) window.attachEvent('onload',myFunction);
---

Then while it behaves exactly the same in IE, in FireFox you get an ugly "[Exception... "'Error: a new error' when calling method: " style error message. This offends me, I want the nice pretty and functional error message.

It must be possible, because if I generate a default error, e.g.:

---
var myFunction = function() {alert(afgad.adfg)}
if(window.addEventListener) window.addEventListener('load',myFunction,false);
else if(window.attachEvent) window.attachEvent('onload',myFunction);
---

It works properly again.

Does anyone know if there is a technique for throwing this error properly from within an event listener for FireFox?

Thanks.
Robin.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros