我有以下 coffeescript 代码:
try
do something
catch error
log something
throw error
我应该使用 throw new Error(error)
而不是 throw error
吗?
有什么区别?
最佳答案
与 C# 或 Java 等其他语言相同:
throw error
抛出相同的错误对象throw new Error(error)
将其包装到一个新的 Error 对象中。后者用于例如在 Java 中需要转换 checked Exception 时。进入未经检查的一个。在 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
因此,正如您所见,通过使用包装错误对象,我们可以隐藏原始错误的参数
。假设您正在编写以下内容之一:
- 某种图书馆
- 将在您不拥有的页面上加载的脚本(例如,某种点赞或推文按钮)
- 页面上加载了一些第三方脚本(社交按钮、广告、跟踪代码等)的脚本
在上面列出的所有这些情况下,为了保持安全,您应该包装您的 错误
对象。否则,您可能会意外泄漏对内部对象、函数和变量的引用。
编辑 2:关于堆栈跟踪。两种变体都保留了它们。这是 a working example我在 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
关于javascript - 使用 coffeescript/javascript 'throw error' 还是 'throw new Error(error)'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10977120/