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

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

问题描述

我想编写一个通用的错误处理程序,它可以捕获在代码的任何实例中故意抛出的自定义错误.

当我像下面的代码那样throw new Error('sample')

尝试{throw new Error({'hehe':'haha'});//抛出新错误('嘿嘿');}赶上(e){警报(e);控制台日志(e);}

日志在 Firefox 中显示为 Error: [object Object] 并且我无法解析该对象.

对于第二个throw,日志显示为:Error: hehe

当我这样做时

尝试{抛出({'呵呵':'哈哈'});}赶上(e){警报(e);控制台日志(e);}

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

有什么区别?

区别是否如代码所示?就像字符串只会作为字符串和对象作为对象传递,但语法会不同吗?

我还没有探索过抛出错误对象……我只做过抛出字符串.

除了上面提到的两种方法还有其他方法吗?

解决方案

javascript 中 'throw new Error' 和 'throw someObject' 的区别在于 throw new Error 将传递给它的错误包装成以下格式 -<块引用>

{ name: 'Error', message: 'String you pass in the constructor'}

throw someObject 将按原样抛出对象,并且不允许从 try 块中进一步执行任何代码,即与 throw new Error 相同.

这里有一个关于错误对象和抛出你自己的错误的很好解释>

错误对象

如果发生错误,我们可以从中提取什么?所有浏览器中的 Error 对象都支持以下两个属性:

  • name:错误的名称,或者更具体地说,错误所属的构造函数的名称.

  • message:错误描述,此描述因浏览器而异.

name 属性可以返回六个可能的值,如前所述,它们对应于错误构造函数的名称.它们是:

错误名称说明EvalError eval() 函数中出现错误.RangeError 数值超出范围.ReferenceError 发生了非法引用.SyntaxError eval() 函数内的代码出现语法错误.所有其他语法错误都不会被 try/catch/finally 捕获,并且会触发与错误关联的默认浏览器错误消息.要捕获实际的语法错误,您可以使用 onerror 事件.TypeError 预期变量类型中出现错误.URIError 编码或解码 URI 时发生错误(即:调用 encodeURI() 时).

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

在控制自动从 try 块转移到 catch 块之前等待 6 种类型的错误之一发生,您还可以显式抛出自己的异常以强制按需发生.这对于创建您自己的错误定义以及何时应将控制权转移到 catch 非常有用.

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?

解决方案

The difference between 'throw new Error' and 'throw someObject' in javascript is that throw new Error wraps the error passed to it in the following format −

{ name: 'Error', message: 'String you pass in the constructor' }

The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same as throw new Error.

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天全站免登陆