打字稿中的 "throw(e)" 和 "throw e" 有区别吗?

Is there a difference between "throw(e)" and "throw e" in typescript?

我找不到关于打字稿中抛出语法的任何信息,所以我只是想知道这两者在语义上是否相同?从我测试过的两种方法来看,保留调用堆栈。

try {
    throw new Error("You can't recover from this.");
} catch (e) {
    console.error(e);
    throw e;
}
try {
    throw new Error("You can't recover from this.");
} catch (e) {
    console.error(e);
    throw(e);
}

在 C# 中这样做会破坏调用堆栈,所以我只想绝对确定。正确的 C# 方法是这样的:

try {
    throw new Exception("You can't recover from this.");
catch (Exception e)
    _logger.LogError(e);
    throw;
}

throw 在 TypeScript 中(在 javascript 中)可以抛出任意表达式。与ifwhilefor不同,throw后面可以紧跟表达式,而不用包围().

中的表达式

从表达式语义的角度来看,这两个表达式是相同的:

e

(e)