JavaScript------Throw和Try-Catch的使用

时间:2023-03-09 22:58:24
JavaScript------Throw和Try-Catch的使用
function test()
{
  try {
    var x = document.getElementById("demo").value;
    if (x == "") throw "值为空";
    if (isNaN(x)) throw "不是数字";
    if (x > 10) throw "太大";
    if (x < 5) throw "太小";
  }
  catch (err) {
    var y = document.getElementById("demo");
    y.innerHTML = "错误:" + err + "。";
  }
}