尝试在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:39:32  来源:igfitidea点击:

try catch error in javascript - get more error details

javascript

提问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 .nameproperty 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 .namecan 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 .nameproperty.

名称描述了错误的类型,其值.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.nameand err.message.

基本上你有err.nameerr.message

You also have a few vendor-specific extensions:

您还有一些特定于供应商的扩展:

Microsoft => err.descriptionand err.number.

微软 =>err.descriptionerr.number.

Mozilla => err.fileName, err.lineNumberand err.stack.

的Mozilla => err.fileNameerr.lineNumbererr.stack

回答by samba

function message()
{
  try
  {
  }
  catch(err)
  {
   alert(err.message); 
 }
} 

SEE HEREand HERE

看到这里这里