第四课--js错误处理

59 阅读1分钟

14.错误处理
try 语句测试代码块的错误。
catch 语句处理错误。
throw 语句创建自定义错误。
finally 语句在 try 和 catch 语句之后,无论是否有触发异常,该语句都会执行。

try {
...    //异常的抛出
} catch(e) {
...    //异常的捕获与处理
} finally {
...    //结束处理
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
    try{
       add()
        
    } catch(e){
        alert(e)
    }finally{
        var a = 1;
        console.log(a)
    }    
    </script>
</body>
</html>