Ole
Ole

Reputation: 47058

Get the method that threw the exception?

How do we get the method that threw the exception in Javascript?

For example:

function method()
try { throw new Error('oh oh')}
catch(e) { e.stack... how to get the method ...}

Tried:

      console.log("The method is: " + e.method);

But it comes up as undefined.

Upvotes: 0

Views: 581

Answers (2)

strblr
strblr

Reputation: 950

This library might suit your needs : http://www.eriwen.com/javascript/stacktrace-update/

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370979

The .stack property will contain a string showing the stack trace - the first line will be the error thrown, the second line will list the containing function name (if any) and line / column number that threw the error:

function method() {
  try {
    throw new Error('oh oh')
  } catch (e) {
    console.dir(e.stack.split('\n')[1]);
  }
}
method();

Upvotes: 2

Related Questions