-1

I am trying to throw an exception in javascript, I am trying to give exception a number along with the message, but i am only getting the message alone and not the number why? what am i doing wrong? my code is:

function ain()
{
    var e = new Error("hello guys" ,12345);
    throw e;
}

(function() {
    try {
       ain();
    }
    catch(e) {
        alert(e.number+"     "+e.message);
    }
}());

The output is :

undefined     hello guys
Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Mr_Hmp
  • 2,474
  • 2
  • 35
  • 53
  • 1
    This might help: http://stackoverflow.com/a/1137209/921204 – techfoobar Mar 21 '13 at 13:23
  • 1
    There is no `number` member of Error -- see https://developer.mozilla.org/en/docs/JavaScript/Reference/Global_Objects/Error – Noyo Mar 21 '13 at 13:23
  • 1
    @Noyo There is a number member for error in Internet Explorer 10. Reference: http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx – CyclingFreak Mar 21 '13 at 13:47
  • 1
    @JurgenStillaert It is non-standard: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Error/prototype – Noyo Mar 21 '13 at 13:53

4 Answers4

2

The Error constructor function does not take a "number" property. It only takes a string message (the other two parameters are non-standard so I will not mention them).

If you really need to use a fully customized error object, you can just throw an object literal instead of an error:

try {
    throw { message: 'hello world', number: 12345};
} catch(e) {
    console.log(e.number + ': ' + e.message);
}

http://jsfiddle.net/FHc7v/1/

jbabey
  • 45,965
  • 12
  • 71
  • 94
2

If you want to pass a number that isn't a line number as a second parameter, you might want to consider creating a new error type, that inherits from the main Error prototype:

function MyError(msg, num)
{
    this.name = 'MyError';
    this.message = msg;
    this.number = num || 0;
}
MyError.prototype = new Error;
MyError.prototype.constructor = MyError;
//and if you want:
MyError.prototype.alertMsg = function()
{
    alert(this.name + ': ' + this.message + ' -- ' + this.number);
};

This augmented error object can be used as you want:

function iThrow()
{
    throw new MyError('I throw errors with numbers', 123);
}
try
{
    iThrow();
}
catch(e)
{
    e.alertMsg();//alerts "MyError: I throw errors with numbers -- 123"
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Here you can find more information, and it is also browser specific:

http://help.dottoro.com/ljfhismo.php

CyclingFreak
  • 1,614
  • 2
  • 21
  • 40
-1

If you look the description for this method You will see that second parameter is file name not number and third option is line number of error not error code.

new Error([message[, fileName[, lineNumber]]])
Mihkel Selgal
  • 508
  • 6
  • 10
  • 1
    You can use `Exception` if you want to, but I'd prefer using constructors/prototypes that actually exist. In JS there is no such thing as an `Exception` prototype... – Elias Van Ootegem Mar 21 '13 at 13:46