If you throw
in JavaScript, the thrown error will usually propagate to the window.onerror
handler which can stop the further execution of a script.
Is there any way to get a stack trace from within a function without causing this halting of execution?
You can also just create a new error without throwing it and use the stack trace
function doSomething() {
...
const stackTrace = new Error().stack
...
}
Throwing an error will halt the stack unless caught by a try/catch.
function getStack() {
try {
throw new Error();
} catch(e) {
return e.stack;
}
}
Invoking getStack from within any function will print out the stack from there.
Note, the method names in the stack are not affected by sourcemaps, so if you're dealing with minified code you might still get obfuscated names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.