获取 Windows Script Host 中运行的 JScript 中错误的行号

发布于 2024-12-05 15:49:51 字数 181 浏览 2 评论 0原文

假设我有以下代码,使用 Windows 脚本宿主作为 .JS 文件运行:

try
{
    ProduceAnError();
}
catch(e)
{
    //How to get an error line here?
}

有什么方法可以知道发生错误(异常)的错误行吗?

Say, I have the following code that I run as a .JS file using the Windows Script Host:

try
{
    ProduceAnError();
}
catch(e)
{
    //How to get an error line here?
}

Is there any way to know the error line where an error (exception) occurred?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2024-12-12 15:49:51

抱歉我的其他回复。这不是很有帮助:P

我相信您正在寻找的是 ReferenceError 的堆栈属性。您可以使用传递给 catch:

try {
  someUndefinedFunction("test");
} catch(e) {
  console.log(e.stack)
}

示例输出的参数来访问它:

 ReferenceError: someUndefinedFunction is not defined
     at message (http://example.com/example.html:4:3)
     at <error: TypeError: Accessing selectionEnd on an input element that cannot have a selection.>
     at HTMLInputElement.onclick (http://example.com/example.html:25:4)

Sorry for my other reply. It wasn't very helpful :P

I believe what you are looking for is the ReferenceError's stack property. You can access it with the argument that you pass to the catch:

try {
  someUndefinedFunction("test");
} catch(e) {
  console.log(e.stack)
}

example output:

 ReferenceError: someUndefinedFunction is not defined
     at message (http://example.com/example.html:4:3)
     at <error: TypeError: Accessing selectionEnd on an input element that cannot have a selection.>
     at HTMLInputElement.onclick (http://example.com/example.html:25:4)
悲喜皆因你 2024-12-12 15:49:51

确实没有办法以编程方式获取堆栈甚至捕获行号。抛出错误时只能看到行号。您最多可以获得错误代码和描述,或者您可以自己犯错误。

这是文档

举个例子

try {
    produceAnError();
} catch(e) {
    WScript.echo((e.number>>16 & 0x1FFF)); // Prints Facility Code
    WScript.echo((e.number & 0xFFFF));     // Prints Error Code
    WScript.echo(e.description);           // Prints Description
    throw e;
}

There isn't really a way to get a stack or even catch the line number programmatically. One can only see the line number when throwing an error. At best you can get the error code and description or you can make your own errors.

Here's the documentation.

And an example

try {
    produceAnError();
} catch(e) {
    WScript.echo((e.number>>16 & 0x1FFF)); // Prints Facility Code
    WScript.echo((e.number & 0xFFFF));     // Prints Error Code
    WScript.echo(e.description);           // Prints Description
    throw e;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文