try,catch捕获错误的用法

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <script>
        var txt="";
        function message(aa)
        {
            try
            {
                txt = JSON.parse(aa)
            }
            catch(err)
            {
                txt="本页有一个错误。\n\n";
                txt+="错误描述:" + err.message + "\n\n";
                txt+="点击确定继续。\n\n";
                alert(txt);
            }
            return txt;
        }
    </script>
</head>

<body>
<input type="button" value="查看消息" onclick="message()" />
</body>

</html>

 

posted @ 2016-03-23 18:24  挥刀  阅读(179)  评论(0编辑  收藏  举报