【问题标题】:Unexpected 'undefined' output from 'throw' statement来自“throw”语句的意外“未定义”输出
【发布时间】:2019-12-10 18:22:10
【问题描述】:

我写了一个简单的函数来检查传递的参数是0、正数还是负数。我知道传递的参数是一个数字而不是任何其他数据类型。我得到 undefined 作为输出,而我希望在传递的参数是零或负数的情况下打印 throw 语句中提供的文本。

  1. 我已经检查了Throw statement in JavaScript gives "undefined undefined" output 的问题, 这并不能解决我的问题。

  2. 我还尝试定义一个错误对象,如下所示:

    ZeroError = new Error ("Zero Error");
    
    NegativeError = new Error ("Negative Error");
    

然后将这些错误用作“抛出”的参数:

throw ZeroError;

throw NegativeError;

对于零值和负值,我得到相同的 undefined 输出。

这是我的功能:

function isPositive(a)
{
    if (a === 0) throw "Zero Error";
    if (a < 0) throw "Negative Error";
    if (a > 0) return ("YES");
}

当 a > 0 时我得到“YES”作为输出,我得到 undefined 当 a 为零或负数时。当 a 为零时,我期望“零错误”,当 a 为负数时,我期望“负错误”。

【问题讨论】:

  • 你在哪里打电话isPositive?你从哪里得到你描述的输出,你在某处记录一些东西吗?
  • 可能你正在使用let ??
  • 有点跑题了,但请注意,您通常应该是throwing 错误对象而不是字符串(如throw new Error('Zero Error')
  • @jlaitio:我也试过了,但我得到了“未定义”。

标签: javascript undefined throw


【解决方案1】:

throw 不返回任何内容,它会引发异常,将其替换为return,您将获得您的价值。

throw 语句引发用户定义的异常。当前函数的执行将停止(throw 之后的语句不会被执行),控制权将传递给调用堆栈中的第一个 catch 块。如果调用函数之间不存在 catch 块,程序将终止。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

编辑:如果您需要捕获异常,请查看try/catch:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch

function isPositive(a) {
    if (a === 0) throw "Zero Error";
    if (a < 0) throw "Negative Error";
    if (a > 0) return ("YES");
}

let result; 
try {
   result = isPositive(0);
} catch (e) {
   if (e === "Zero Error") result = "Zero Error";
   if (e === "Negative Error") result = "Negative Error";
}

【讨论】:

  • 我特别需要在零或负数的情况下抛出异常。
  • 那么您的代码将需要尝试/捕获 - 显示您如何使用 isPositive
  • 那你需要添加一个catch并在那里处理
  • 'catch' 与获取 'undefined' 输出有什么关系?
  • @VenkatRamakrishnan 我添加了一个用法示例。
【解决方案2】:
function Positive(a)
{
  try { 
    if (a === 0)
      throw "Zero Error";
    if (a < 0) throw "Negative Error";
    if (a > 0) return ("YES");
    document.write("hello")
  }
  catch(err) {
   document.write("hello");
}
}
Positive();

【讨论】:

  • 编辑了答案以修改“document.write”部分以返回错误。
【解决方案3】:

这行得通:

function isPositive(a) {
    try {
        if (a < 0) {
            throw "Negative Error"
        } else
            if (a == 0) {
                throw "Zero Error";
            } else {
                return "YES"
            }
    }
    catch (err) {
        return err;
    }
}
  1. 需要try-catch
  2. 从捕获中返回错误很重要。那个回报 of error 将解决“未定义”返回。

【讨论】:

    【解决方案4】:

    这对我来说非常有效:

    function isPositive(a) {
        if(a>0) {
            return 'YES';
        }
        if (a===0) {
            throw new Error("Zero Error");
        }
        else if (a<0) {
            throw new Error("Negative Error");
        }
    }
    

    不知道使用 throw 有什么问题,只是它的方式。稍后当我找到真正的原因时,我会在这里添加它。

    https://humanwhocodes.com/blog/2009/03/10/the-art-of-throwing-javascript-errors-part-2/

    【讨论】:

      【解决方案5】:

      问题的优化解决方案。我希望每个人都清楚:)

      function isPositive(a) {
          try {
              if (a == 0) throw 'Zero Error';
              if (a < 0) throw 'Negative Error';
              return 'YES';
          } catch (throwedErrorMessage) {
              return throwedErrorMessage;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-01
        • 2021-07-26
        • 2015-08-31
        • 1970-01-01
        • 2021-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多