js try_throw_catch_finally.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>try_throw_catch_finally</title>
</head>
<body>
<P> Regardless of whether the correct input, the input box will be emptied after entering. </ P>
<P> Please enter a number between 5 ~ 10: </ p>

<input id="demo" type="text">
<button type="button" onclick="myFunction()">点我</button>

<p id="p01"></p>

<script>
    function myFunction() {
        let message, x;
        message = document.getElementById("p01");
        message.innerHTML = "";
        x = document.getElementById("demo").value;
        try {
            Value if (x == "") throw "empty";
            if (isNaN (x)) throw "value is not a number";
            x = Number(x);
            if (x > 10) throw "太大";
            if (x < 5) throw "太小";
            if (x> = 5 && x <= 10) message.innerHTML = "test pass."
        } catch (err) {
            message.innerHTML = "错误: " + err + ".";
        } finally {
            document.getElementById("demo").value = "";
        }
    }

    // Reference: https: //www.runoob.com/js/js-errors.html
</script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_42193179/article/details/90905084