How can I get a JavaScript stack trace when I throw an exception?

David Wolever

If I throw a JavaScript exception myself (eg, throw "AArrggg"), how can I get the stack trace (in Firebug or otherwise)? Right now I just get the message.

edit: As many people below have posted, it is possible to get a stack trace for a JavaScript exception but I want to get a stack trace for my exceptions. For example:

function foo() {
    bar(2);
}
function bar(n) {
    if (n < 2)
        throw "Oh no! 'n' is too small!"
    bar(n-1);
}

When foo is called, I want to get a stack trace which includes the calls to foo, bar, bar.

Eugene Morozov

Edit 2 (2017):

In all modern browsers you can simply call: console.trace(); (MDN Reference)

Edit 1 (2013):

A better (and simpler) solution as pointed out in the comments on the original question is to use the stack property of an Error object like so:

function stackTrace() {
    var err = new Error();
    return err.stack;
}

This will generate output like this:

DBX.Utils.stackTrace@http://localhost:49573/assets/js/scripts.js:44
DBX.Console.Debug@http://localhost:49573/assets/js/scripts.js:9
.success@http://localhost:49573/:462
x.Callbacks/c@http://localhost:49573/assets/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost:49573/assets/js/jquery-1.10.2.min.js:4
k@http://localhost:49573/assets/js/jquery-1.10.2.min.js:6
.send/r@http://localhost:49573/assets/js/jquery-1.10.2.min.js:6

Giving the name of the calling function along with the URL, its calling function, and so on.

Original (2009):

A modified version of this snippet may somewhat help:

function stacktrace() { 
  function st2(f) {
    return !f ? [] : 
        st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']);
  }
  return st2(arguments.callee.caller);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make program print a stack trace automatically when an Exception occurs in Android

From Java

How can I get the current stack trace in Java?

From Dev

java - How can I get a future's stack trace on TimeoutException

From Dev

How do I see the full stack trace when I catch an exception

From Java

How can I convert a stack trace to a string?

From Dev

How can I get the "Source Error" from a stack trace like Asp.Net does?

From Dev

How can I get a method to throw a custom exception I've created?

From Dev

How can I get a method to throw a custom exception I've created?

From Dev

How can I trace MY javascript?

From Dev

How can an Exception be created/thrown with no stack trace?

From Dev

How can I extract local variables from a stack trace?

From Dev

Why am I not seeing a stack trace with 'throw new Error'?

From Dev

How can I display Exception trace in laravel console command?

From Dev

How can I make apigility display exception trace in ApiProblem responses?

From Dev

How can I make apigility display exception trace in ApiProblem responses?

From Dev

how can I throw an exception when casting from string to int caused precision loss?

From Dev

how can I throw an exception when casting from string to int caused precision loss?

From Dev

How can I get wstring_convert::to_bytes to throw a range_error exception?

From Dev

How do I get a PHP full call trace, not only stack call trace?

From Java

How can I manually return or throw a validation error/exception in Laravel?

From Dev

How can I throw a 403 exception in Symfony2?

From Dev

How can I return None or throw an exception if no implicit defined?

From Dev

How can I mock a void method to throw an exception?

From Dev

How can I create a new exception class and throw it

From Dev

How can i find why RestSharp PUT throw Exception?

From Dev

How can i throw exception if processing don't response

From Dev

How can I make an incorrect pointcut expression throw an exception?

From Dev

How can I define a Stream with a "throw exception" value?

From Dev

How can I throw exception inside a Generic Type Method?

Related Related

  1. 1

    How can I make program print a stack trace automatically when an Exception occurs in Android

  2. 2

    How can I get the current stack trace in Java?

  3. 3

    java - How can I get a future's stack trace on TimeoutException

  4. 4

    How do I see the full stack trace when I catch an exception

  5. 5

    How can I convert a stack trace to a string?

  6. 6

    How can I get the "Source Error" from a stack trace like Asp.Net does?

  7. 7

    How can I get a method to throw a custom exception I've created?

  8. 8

    How can I get a method to throw a custom exception I've created?

  9. 9

    How can I trace MY javascript?

  10. 10

    How can an Exception be created/thrown with no stack trace?

  11. 11

    How can I extract local variables from a stack trace?

  12. 12

    Why am I not seeing a stack trace with 'throw new Error'?

  13. 13

    How can I display Exception trace in laravel console command?

  14. 14

    How can I make apigility display exception trace in ApiProblem responses?

  15. 15

    How can I make apigility display exception trace in ApiProblem responses?

  16. 16

    how can I throw an exception when casting from string to int caused precision loss?

  17. 17

    how can I throw an exception when casting from string to int caused precision loss?

  18. 18

    How can I get wstring_convert::to_bytes to throw a range_error exception?

  19. 19

    How do I get a PHP full call trace, not only stack call trace?

  20. 20

    How can I manually return or throw a validation error/exception in Laravel?

  21. 21

    How can I throw a 403 exception in Symfony2?

  22. 22

    How can I return None or throw an exception if no implicit defined?

  23. 23

    How can I mock a void method to throw an exception?

  24. 24

    How can I create a new exception class and throw it

  25. 25

    How can i find why RestSharp PUT throw Exception?

  26. 26

    How can i throw exception if processing don't response

  27. 27

    How can I make an incorrect pointcut expression throw an exception?

  28. 28

    How can I define a Stream with a "throw exception" value?

  29. 29

    How can I throw exception inside a Generic Type Method?

HotTag

Archive