5

Say, I have the following code that I run as a .JS file using the Windows Script Host:

try
{
    ProduceAnError();
}
catch(e)
{
    //How to get an error line here?
}

Is there any way to know the error line where an error (exception) occurred?

Kev
  • 118,037
  • 53
  • 300
  • 385
ahmd0
  • 16,633
  • 33
  • 137
  • 233

2 Answers2

0

There isn't really a way to get a stack or even catch the line number programmatically. One can only see the line number when throwing an error. At best you can get the error code and description or you can make your own errors.

Here's the documentation.

And an example

try {
    produceAnError();
} catch(e) {
    WScript.echo((e.number>>16 & 0x1FFF)); // Prints Facility Code
    WScript.echo((e.number & 0xFFFF));     // Prints Error Code
    WScript.echo(e.description);           // Prints Description
    throw e;
}
Aerze
  • 1
0

Sorry for my other reply. It wasn't very helpful :P

I believe what you are looking for is the ReferenceError's stack property. You can access it with the argument that you pass to the catch:

try {
  someUndefinedFunction("test");
} catch(e) {
  console.log(e.stack)
}

example output:

 ReferenceError: someUndefinedFunction is not defined
     at message (http://example.com/example.html:4:3)
     at <error: TypeError: Accessing selectionEnd on an input element that cannot have a selection.>
     at HTMLInputElement.onclick (http://example.com/example.html:25:4)
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • Thanks. Your method works fine in a browser (Firefox for instance), but e.stack is undefined where I need it -- when I run my .JS file through the Windows Script Host :( – ahmd0 Sep 18 '11 at 04:05