尝试在 javascript 中捕获错误 - 获取更多错误详细信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16194510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
try catch error in javascript - get more error details
提问by Hello-World
How can I get more error details from a javascript catch?
如何从 javascript 捕获中获取更多错误详细信息?
Are there more parameters to get more details from the caught error.
是否有更多参数可以从捕获的错误中获取更多详细信息。
try {
var s = null;
var t = s.toString();
} catch(err) {
alert(err);
}
回答by KernelPanik
The Error Objecthas several properties that you can use. One property you can use to get the message of the error, is .message
, as in:
该错误对象有几个属性,你可以使用。您可以用来获取错误消息的一个属性是.message
,如下所示:
catch(err) {
alert(err.message);
}
The .name
property returns the type of error as in:
该.name
属性返回错误类型,如下所示:
catch(err) {
x = err.name;
// ... do something based on value of x
}
The name describes the type of error, and the value of .name
can be : EvalError, RangeError, ReferenceError, SyntaxError, TypeError
, and URIError
. You may decide to handle the error differently depending on the error type which is returned by the .name
property.
名称描述了错误的类型,其值.name
可以是 : EvalError, RangeError, ReferenceError, SyntaxError, TypeError
、 和 URIError
。您可以决定根据.name
属性返回的错误类型以不同方式处理错误。
A good tutorial can be found on JavaScriptKit. The is also an article on the error object at Mozilla Developer Network.
在JavaScriptKit上可以找到一个很好的教程。这也是Mozilla Developer Network上关于错误对象的文章。
回答by MMeersseman
Check this link out: Reference to Error.prototype
查看此链接: 参考 Error.prototype
Basically you have err.name
and err.message
.
基本上你有err.name
和err.message
。
You also have a few vendor-specific extensions:
您还有一些特定于供应商的扩展:
Microsoft => err.description
and err.number
.
微软 =>err.description
和err.number
.
Mozilla => err.fileName
, err.lineNumber
and err.stack
.
的Mozilla => err.fileName
,err.lineNumber
和err.stack
。