Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript exception stack trace

In Firefox I can get the stack trace of an exception by using exception.stack.

Is there a way to get that in other browsers, too?

Edit: I actually want to save the stack trace automatically (if possible) and not debug it at the time (i.e. I know how to get the stack trace in a debugger).

like image 914
ujh Avatar asked Sep 29 '08 08:09

ujh


People also ask

What is stack trace in exception?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

What is a JavaScript stack trace?

A stack trace is a list of the functions, in order, that lead to a given point in a software program. A stack trace is essentially a breadcrumb trail for your software. You can easily see the stack trace in JavaScript by adding the following into your code: console. trace();

What is Exception explain different ways of handling exceptions in JavaScript?

In exception handling: It means when an abnormal condition occurs, an exception is thrown using throw. The thrown exception is handled by wrapping the code into the try… catch block. If an error is present, the catch block will execute, else only the try block statements will get executed.


3 Answers

Place this line where you want to print the stack trace:

console.log(new Error().stack);

Note: tested by me on Chrome 24 and Firefox 18

May be worth taking a look at this tool as well.

like image 129
Francesco Casula Avatar answered Oct 19 '22 08:10

Francesco Casula


Webkit now has functionality that provides stack traces:

Web Inspector: Understanding Stack Traces, posted by Yury Semikhatsky on Wednesday, April 20th, 2011 at 7:32 am (webkit.org)

From that post:

like image 35
Brian M. Hunt Avatar answered Oct 19 '22 09:10

Brian M. Hunt


If you want the string stack trace, I'd go with insin's answer: stacktrace.js. If you want to access the pieces of a stacktrace (line numbers, file names, etc) stackinfo, which actually uses stacktrace.js under the hood.

like image 3
B T Avatar answered Oct 19 '22 08:10

B T