2.11 错误处理

  • try throw catch
<script>
    var a = prompt("输入数字");
    try {
        if (a < 0) {
            throw new Error("小于 0");
        }
        document.write(Math.sqrt(a));
    }
    catch(e) {
        alert(e.message);
    }
</script>