Javascript-在错误处理情况下抛出异常

问题描述

我一直在尝试定义一个检查数字是否为正的函数,我想使用“ throw”,但是我没有找到一种使“ throw”起作用的方法。我也尝试在“ try”块中使用“ throw”,但是它也不起作用。所以我的问题是...

为什么这样做:

    function isPositive(a) {


    if (a < 0) {
        return 'Negative Number';
    }

    if (a == 0) {
        return 'Codename Error Zero'
    }

    try {
        return 'It IS a positive number!';
    }
    catch(e) {
        console.log(e);
    }
}

这不是:

    function isPositive(a) {


    if (a < 0) {
        throw 'Negative Number';
    }

    if (a == 0) {
        throw 'Codename Error Zero'
    }

    try {
        return 'It IS a positive number!';
    }
    catch(e) {
        console.log(e);
    }

}

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)