为什么我的javascript try / catch块没有触发?

时间:2014-09-05 15:01:54

标签: javascript html function try-catch

除非我完全注释掉try / catch代码,否则我的函数中的代码都不会运行。这是为什么?我会在try / catch之前和之后以及在try或catch中添加代码,并且什么都不会运行。

<html>
<head>
<script type="text/javascript">
var y=3;
function message()
{
    try
    {
        alert("fdgg");
    }
    catch(err)
    {
        txt="Something went wrong, deal with it!"\n\n";
        txt+="Error crap " + err.message + "\n\n";
        txt+="Click Ok to do stuff.";
        alert(txt);
    }
    alert("67");
}

</script>
</head>
<body>
<input type="button" value="Pooper" onclick="message()");
</body>
</html>

1 个答案:

答案 0 :(得分:3)

更改

txt="Something went wrong, deal with it!"\n\n";

txt="Something went wrong, deal with it!\n\n";