9

我有以下咖啡脚本代码:

try
   do something
catch error
   log something
   throw error

我应该使用throw new Error(error)而不是throw error吗?

有什么区别?

4

1 回答 1

17

与 C# 或 Java 等其他语言相同:

  • throw error抛出相同的错误对象
  • throw new Error(error)将其包装到一个新的 Error 对象中。后者用于,例如,在 Java 中,当您需要将已检查的异常转换为未检查的异常时。在 JavaScript 中,您不需要包装异常,因为这会使堆栈跟踪变得更长且不那么漂亮。

编辑:还有一些安全隐患。这是一个例子:

function noWrap() {
    try {
        var o = {}; o.nonexistingMethod();
    } catch (error) {
        throw error;
    }
}

function wrap() {
    try {
        var o = {}; o.nonexistingMethod();
    } catch (error) {
        throw new Error(error);
    }
}

调用noWrap()会产生以下错误消息:

"TypeError: Object #<Object> has no method 'nonexistingMethod'"
// with error.arguments === ['nonexistingMethod', o]

调用wrap()会产生以下错误消息:

"Error: TypeError: Object #<Object> has no method 'nonexistingMethod'"
//  with error.arguments === undefined

因此,正如您所看到的,通过使用包装 Error 对象,我们可以隐藏arguments原始错误。假设您正在编写以下内容之一:

  • 某种图书馆
  • 将加载到您不拥有的页面上的脚本(例如,某种喜欢或推文按钮)
  • 页面上加载了一些第三方脚本(社交按钮、广告、跟踪代码等)的脚本

在上面列出的所有这些情况下,为了保持安全,您应该包装您的 Error 对象。否则,您可能会意外泄漏对内部对象、函数和变量的引用。

编辑 2:关于堆栈跟踪。两种变体都保留了它们。这是一个工作示例,我在 Chrome 中得到以下堆栈跟踪:

// No wrapping:
TypeError: Object #<Object> has no method 'nonexistingMethod'
    at noWrap (http://fiddle.jshell.net/listochkin/tJzCF/show/:22:23)
    at http://fiddle.jshell.net/listochkin/tJzCF/show/:37:5
    at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3901:62
    at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3915:20

// Wrapping:
Error: TypeError: Object #<Object> has no method 'nonexistingMethod'
    at wrap (http://fiddle.jshell.net/listochkin/tJzCF/show/:32:15)
    at http://fiddle.jshell.net/listochkin/tJzCF/show/:44:5
    at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3901:62
    at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3915:20 
于 2012-06-11T09:15:16.377 回答