在Chrome中从控制台运行代码时获取堆栈跟踪错误 [英] Getting stack trace for an error when running code from the console in Chrome

查看:935
本文介绍了在Chrome中从控制台运行代码时获取堆栈跟踪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从控制台调用一个函数,但是当它抛出一个异常时,我没有收到类似于代码正常执行的堆栈跟踪。



是有一种方法可以修改我的命令(也许用try / catch)让它提供这些信息吗?



澄清:

page.js:

  function otherStuff(){return ['a','b',undefined,'c']; 
函数doStuff(){
var x = otherStuff();
var z = parseInt(x [2]); //这将会抛出一个错误
}

page.js

 >其他的东西(); 

我没有收到返回给我的错误的行号。当从页面(而不是控制台)运行它时,我会收到一个行号和一个堆栈跟踪。

解决方案

虽然详细,但会在Chrome JS控制台中打印交互式错误的堆栈跟踪:

  try {
throw new Error();
} catch(e){
console.error(e.stack);
}

不幸的是,如果非错误对象被抛出。


I am calling a function from the console but when it throws an exception I do not receive a stack trace like I would if the code was executed normally.

Is there a way I can modify my command (perhaps with try/catch) to have it provide me this information?

to clarify:

page.js:

function otherStuff() { return ['a','b',undefined,'c'];
function doStuff() {
    var x = otherStuff();
    var z = parseInt(x[2]); // this will throw an error
}

console, after loading a html page that links page.js

> otherStuff();

I get no line number from the Error that is returned to me. When running it from the page (instead of the console) i would receive a line number and a stack trace.

解决方案

Although verbose, this will print the stack trace of an interactive error in the Chrome JS console:

try { 
    throw new Error(); 
} catch (e) { 
    console.error(e.stack); 
}

Unfortunately this won't work if a non-Error object is thrown.

这篇关于在Chrome中从控制台运行代码时获取堆栈跟踪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆