Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript exception handling - displaying the line number

When catching / handling exceptions in JavaScript, how can I determine what the call stack was when the exception occurred? (and also if possible what the line number was)

try {     // etc... } catch (ex) {     // At this point here I want to be able to print out a detailed exception      // message, complete with call stack, and if possible line numbers. } 
like image 421
Justin Avatar asked Dec 14 '09 13:12

Justin


People also ask

How can I get the line number which threw exception?

Simple way, use the Exception. ToString() function, it will return the line after the exception description. You can also check the program debug database as it contains debug info/logs about the whole application.

Is exception handling possible in JavaScript?

As with many programming languages, the primary method of dealing with exceptions in JavaScript is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution.


2 Answers

Each browser handles this differently, so there isn't a universal way to do it. This blog post has some good code to dump a stack trace for most supported browsers. I don't think there is a good way to provide the line number.

If you're looking to debug one function in particular, Firebug has a good stack trace function (vis console.trace()).

like image 64
Chris Clark Avatar answered Sep 20 '22 03:09

Chris Clark


Have a look at this.

A way to analyse the available information:

try  {      doInit();  } catch(err)  {      var vDebug = "";      for (var prop in err)      {          vDebug += "property: "+ prop+ " value: ["+ err[prop]+ "]\n";      }      vDebug += "toString(): " + " value: [" + err.toString() + "]";      status.rawValue = vDebug;  } 
like image 36
jldupont Avatar answered Sep 18 '22 03:09

jldupont