“throw new Error”和“throw someObject”有什么区别? [英] What is the difference between `throw new Error` and `throw someObject`?

查看:729
本文介绍了“throw new Error”和“throw someObject”有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我做了抛出新的错误('sample'),如下面的代码

  try {
抛出新的错误({'hehe':'haha'});
// throw new Error('hehe');
} catch(e){
alert(e);
console.log(e);
}

日志在Firefox中显示为错误:[object Object ] ,我无法解析对象。



对于第二个 throw 日志显示为:错误:hehe



而当我做了

  try {
throw({'hehe':'haha'});
} catch(e){
alert(e);
console.log(e);
}

控制台显示为: Object {hehe =哈哈} ,我可以在其中访问错误属性。



有什么区别? p>

代码中看到的差异?像字符串一样,字符串和对象作为对象传递,但是语法将不同?



我没有探索过抛出错误对象...我只做了一些扔字符串。



有没有其他方法比上述两种方法?

解决方案

p>这里是关于错误对象并抛出您自己的错误的一个很好的解释



错误对象



只要我们可以从错误中提取出来。所有浏览器中的Error对象支持以下两个属性:




  • name:错误的名称,或更具体地说,


  • 消息:错误的描述,其描述因浏览器的不同而异。




名称属性可以返回六个可能的值,如上所述,这些值与错误的构造函数的名称相对应。它们是:

 错误名称描述

EvalError eval()函数已经发生。

RangeError超出范围数值已发生。

ReferenceError发生非法引用。

SyntaxError eval()函数内的代码中出现语法错误。
所有其他语法错误不会被try / catch / finally捕获,并且
将触发与错误相关联的默认浏览器错误消息。
要捕获实际的语法错误,可以使用onerror事件。

TypeError发生了预期变量类型的错误。

URIError编码或解码URI时发生错误
(即:在调用encodeURI()时)。

抛出您自己的错误(例外)



在控制从try块自动传输到catch块之前,不要等待6种类型的错误之一,您还可以明确地抛出自己的异常,强制按需发生。这对于创建自己的错误定义以及何时应将控件转移到捕获中是非常有用的。


I want to write a common error handler which will catch custom errors thrown on purpose at any instance of the code.

When I did throw new Error('sample') like in the following code

try {
    throw new Error({'hehe':'haha'});
    // throw new Error('hehe');
} catch(e) {
    alert(e);
    console.log(e);
}

Log shows in Firefox as Error: [object Object] and I couldn’t parse the object.

For the second throw the log shows as: Error: hehe

Whereas when I did

try {
    throw ({'hehe':'haha'});
} catch(e) {
    alert(e);
    console.log(e);
}

the console showed as: Object { hehe="haha"} in which I was able to access the error properties.

What is the difference?

Is the difference as seen in the code? Like string will be just passed as string and object as objects but the syntax will be different?

I haven’t explored throwing error object… I had done only throwing strings.

Is there any other way than the above two mentioned methods?

解决方案

here is a good explanation about The Error object and throwing your own errors

The Error Object

just what we can extract from it in an event of an error. The Error object in all browsers support the following two properties:

  • name: The name of the error, or more specifically, the name of the constructor function the error belongs to.

  • message: A description of the error, with this description varying depending on the browser.

Six possible values can be returned by the name property, which as mentioned correspond to the names of the error's constructors. They are:

Error Name          Description

EvalError           An error in the eval() function has occurred.

RangeError          Out of range number value has occurred.

ReferenceError      An illegal reference has occurred.

SyntaxError         A syntax error within code inside the eval() function has occurred.
                    All other syntax errors are not caught by try/catch/finally, and will
                    trigger the default browser error message associated with the error. 
                    To catch actual syntax errors, you may use the onerror event.

TypeError           An error in the expected variable type has occurred.

URIError            An error when encoding or decoding the URI has occurred 
                   (ie: when calling encodeURI()).

Throwing your own errors (exceptions)

Instead of waiting for one of the 6 types of errors to occur before control is automatically transferred from the try block to the catch block, you can also explicitly throw your own exceptions to force that to happen on demand. This is great for creating your own definitions of what an error is and when control should be transferred to catch.

这篇关于“throw new Error”和“throw someObject”有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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