JavaScript exception handling - displaying the line number

Justin picture Justin · Dec 14, 2009 · Viewed 28k times · Source

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.
}

Answer

Chris Clark picture Chris Clark · Dec 14, 2009

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()).