6

I create an error message its working with IE and Mozila. Not woking with Safari, Chrome and Opera.

But I need to use it. Please give me right way for doing it.

<script language="javascript" type="text/javascript">
window.onerror = function(msg, url, line)
{
    document.write("Message\t = "+msg + "<br/>Location\t = " + url + "<br/>Line No.\t = " + line + "<br/>Error No.\t = " + this.err.number);
}
this.err = Error(12,"My Own Error");
throw this.err;
</script>

==========================================
Internet Explorer:

My Error
Message = My Own Error
Location = http://localhost/practice/JavaScript/window.errors.php
Line No. = 8
Error No. = 12 
================================================
Mozilla FireFox:

My Error
Message = Script error.
Location = My Own Error
Line No. = 0
Error No. = undefined 
=====================================================
Safari, Chrome, Opera:

My Error

look the code Mozilla give wrong information. what I do?

1
  • 2
    (Reference) quirksmode.org/dom/events/error.html the event is indeed very badly supported on IE and FF; not sure whether there is anything that can be done about it.
    – Pekka
    Commented Jun 22, 2010 at 12:08

1 Answer 1

4

Opera doesn't support window.onerror at all. Chrome supports it, but not on errors that you throw yourself. This is also true of Internet Explorer when using Error objects other than Error(), e.g. TypeError(). Chrome also doesn't provide the line and file arguments.

You should correctly catch any exceptions you're going to throw with a try...catch statement, instead of relying on window.onerror.

4
  • 7
    The point is to catch exceptions/errors you are NOT going to throw, but which are likely to happen in a sufficiently complex JS (for example, to show a nice apology message to the user). I'm looking at the same problem myself, and Chrome/Safari don't catch invalid reference errors - which are arguably the most frequent.
    – sereda
    Commented Sep 9, 2010 at 15:22
  • @sereda: then I'm afraid you're out of luck, unless you want to wrap each block of code with try and catch statements (please don't :-)). The solution, of course, is to write error free code and test as much as humanly possible.
    – Andy E
    Commented Sep 9, 2010 at 16:33
  • 4
    @AndyE You can only write as bug free code as your 3rd party libraries let you. For example, I ended up here because I have to use the buggy Facebook Javascript library, and the exception they are throwing isn't even catchable via try/catch because it triggers from a dialog they control. Commented Sep 20, 2011 at 19:20
  • 3
    ' The solution, of course, is to write error free code and test as much as humanly possible.'. - The solution is not grounded in reality. Commented Oct 23, 2013 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.