简体   繁体   English

Javascript:“抛出undefined”是否安全?

[英]Javascript: is it safe to “throw undefined”?

In the function below, when the condition fails I want the situation to be handled as a simple error (no details needed). 在下面的函数中,当条件失败时,我希望将情境作为一个简单的错误处理(不需要任何细节)。 Just out of curiosity, is it OK and safe to write throw undefined ? 出于好奇,是不是可以安全地写出throw undefined

function splitYearMonth (YM) { // Returns ["yyyy-mm", yyyy, mm]
  try {
    var o = YM.match(/^(\d{4})\-(0[1-9]|1[012])$/);
    if (o != null) {
      return  [o[0], parseInt(o[1], 10), parseInt(o[2], 10)];
    } else {
      throw undefined;
    }
  } catch (e) {
    return [undefined, undefined, undefined];
  }
}

Yes, it is safe to do so. 是的,这样做是安全的。

The ECMAScript 5.1 specification says : ECMAScript 5.1规范

The production ThrowStatement : throw [no LineTerminator here] Expression ; 生产ThrowStatement抛出 [此处没有LineTerminator ] 表达式; is evaluated as follows: 评估如下:

  1. Let exprRef be the result of evaluating Expression . exprRef是评估Expression的结果。
  2. Return (throw, GetValue( exprRef ), empty). 返回(throw,GetValue( exprRef ),为空)。

ECMAScript 6 uses the same terms . ECMAScript 6 使用相同的术语

undefined is definitely an expression, so it can be thrown. undefined绝对是一个表达式,所以它可以被抛出。 You can see an example in this fiddle . 你可以在这个小提琴中看到一个例子。

That said, throwing undefined may not be a good idea from a maintainability standpoint, because doing so leaves you with no information at all about the cause of the exception. 也就是说,从可维护性的角度来看,抛出undefined可能不是一个好主意,因为这样做会让你完全没有关于异常原因的信息。 Throwing a string would arguably be a better solution: 抛出一个字符串可以说是一个更好的解决方案:

var o = YM.match(/^(\d{4})\-(0[1-9]|1[012])$/);
if (o != null) {
    return [o[0], parseInt(o[1], 10), parseInt(o[2], 10)];
} else {
    throw "unrecognized date format";
}

Update: On second thought, unless the no details needed clause in your question means you're not telling us the whole story, you only need control flow, not exception handling. 更新:第二个想法,除非你的问题中没有需要细节的条款意味着你没有告诉我们整个故事,你只需要控制流程,而不是异常处理。 You only have to write: 你只需要写:

function splitYearMonth(YM) {  // Returns ["yyyy-mm", yyyy, mm]
    var o = YM.match(/^(\d{4})\-(0[1-9]|1[012])$/);
    if (o != null) {
        return [o[0], parseInt(o[1], 10), parseInt(o[2], 10)];
    } else {
        return [undefined, undefined, undefined];
    }
}

Exception handling can be expensive, and it is usually not recommended to use this facility for control flow (since we're discussing Javascript and not, say, Python). 异常处理可能很昂贵,并且通常不建议将此工具用于控制流(因为我们讨论的是Javascript而不是Python)。

The syntax of throw is: throw的语法是:

throw expression;

As undefined is a valid expression, it is safe to do this, although it is generally good practice to return a sensible error message, for example: 由于undefined是一个有效的表达式,所以这样做是安全的,尽管返回一个合理的错误消息通常是一种好习惯,例如:

throw "Failed to split year and month for the given input"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript中的Throw语句提供“ undefined undefined”输出 - Throw statement in JavaScript gives “undefined undefined” output 为什么此javascript会引发未定义的错误? - Why does this javascript throw an undefined error? 如果在没有参数的情况下调用 JavaScript 函数或未定义参数,则 Javascript 函数应引发错误 - Javascript function should throw an error if called without arguments or an argument is undefined 在 JavaScript 中,是否有可能在任何变量设置为未定义时引发错误? - In JavaScript, is it possible to throw an error whenever any variable is set to undefined? Javascript库检查对未定义变量安全的字符串相等性 - Javascript library to check string equality that is safe for undefined variables 如果未定义则抛出错误? - Throw an error if undefined? JSON 在 HTML 中抛出未定义 - JSON throw undefined in HTML 角度错误:安全性未定义 - angular Error: safe is undefined 为什么Javascript中的未定义变量有时会被评估为false,有时会抛出未被捕获的ReferenceError? - Why does an undefined variable in Javascript sometimes evaluate to false and sometimes throw an uncaught ReferenceError? .NET 4上的ASP.NET导致IE11抛出_doPostBack是未定义的javascript错误 - ASP.NET on .NET 4 causing IE11 throw _doPostBack is undefined javascript error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM