5

I need to get fileName, Message, LineNumber etc from a javascript exception. I tried the following code.

try {
  alertt("dddd");
} catch (e) {
  console.log("ExceptionType: "+ e.name);
  console.log("Message: "+ e.message);
  console.log("Line No: "+ e.lineNumber);
}

I got the following result in Mozilla Firefox

ExceptionType: ReferenceError
Message: alertt is not defined
Line No: 4

But the same code gave the following result in Google Chrome, Internet Explorer

ExceptionType: ReferenceError
Message: alertt is not defined
Line No: undefined

It is not giving the Line Number. How to solve this issue? Is there any another method for getting Line number?

I tried e.stack It returns the stack trace as string. It gave me the following output in Google Chrome

 ReferenceError: alertt is not defined
    at message (http://localhost/ems-test/js/test.js:4:4)
    at HTMLDocument.<anonymous> (http://localhost/ems-test/js/test.js:14:2)
    at c (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26036)
    at Object.p.fireWith [as resolveWith] (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26840)
    at Function.x.extend.ready (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:3305)
    at HTMLDocument.q (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:717) 

and firefox gave this result

message@http://localhost/ems-test/js/test.js:4
@http://localhost/ems-test/js/test.js:14
x.Callbacks/c@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
.ready@http://localhost/ems-test/js/jquery-1.10.2.min.js:4
q@http://localhost/ems-test/js/jquery-1.10.2.min.js:4

Both are string type result. Not an object. So it needs to extract the line number from this huge string. But the problem is both result are not same. One shows the line number in first line and another one shows it in second line. So it will be difficult to extract line number from this huge string.

Is there any method to get the stack trace as an object?

Sajith
  • 2,842
  • 9
  • 37
  • 49
  • 3
    The Error object API is not standardized. – Pointy Nov 05 '13 at 04:48
  • is there any other method for getting line number? – Sajith Nov 05 '13 at 04:49
  • I don't know of any way to do it in Internet Explorer or Chrome/Safari, but maybe somebody does. – Pointy Nov 05 '13 at 04:51
  • 1
    Maybe stack contains the line number: e.stack – HMR Nov 05 '13 at 04:57
  • 1
    look into this stacktrace lib https://github.com/eriwen/javascript-stacktrace – Raab Nov 05 '13 at 05:00
  • @Point—the API is [stanardised](http://www.ecma-international.org/ecma-262/5.1/#sec-15.11) (i.e. error objects and their properties are included in the standard), but it may be extended (as may pretty much any native object) by implementations provided such extensions don't contravene ECMA-262. – RobG Nov 05 '13 at 05:02
  • If you are executing from console line number may seem ambiguous. Line number is usually of script/html containing the code and not injected script. The html & script error/line numbering is standardised but for injected script that depends on the browser implementation. – user568109 Nov 05 '13 at 05:09
  • @HMR I tried e.stack. But it is not working in IE. Do you know how to get stack trace in IE? – Sajith Nov 05 '13 at 06:30
  • @RobG sorry yes I meant that beyond the (pretty minimal) standard it's all over the place between browsers. – Pointy Nov 05 '13 at 13:43
  • @Sajith e.stack is Supported in Internet Explorer 10 – user961954 Nov 05 '13 at 19:21
  • The problem is not necessarily the API. In javascript, you can throw *anything*. `try { throw function(){ alert("woo!");}; } catch(e) {e();}` – JayC Nov 21 '13 at 02:16
  • @JayC Do you know how to get line number from this exception in chrome and IE? – Sajith Nov 21 '13 at 04:13

2 Answers2

2
window.onerror = function (msg, url, line) {
   alert("Message : " + msg );
   alert("url : " + url );
   alert("Line number : " + line );
}

Hope this might help you. check on this link: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=javascript_40

robieee
  • 364
  • 2
  • 18
1

I don't have IE on my machine, so I can't speak for that; but in Chrome, you might be able to get what you need by looking at the e.stack property and parsing it out. You can see the options available to you if you do a console.dir(e) within your catch block.

Mike S.
  • 969
  • 5
  • 13