2014-09-05 69 views
-1

我的函數中沒有任何代碼會運行,除非我完全註釋掉try/catch代碼。這是爲什麼?我在try/catch之前和之後不斷添加代碼,並且在try或catch中添加代碼,並且不會運行任何代碼。爲什麼我的javascript 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> 
+3

可能是因爲'警報()'不會引發錯誤,因此沒有趕上? – cdhowie 2014-09-05 15:03:21

+3

也是這個字符串'txt =「出錯了,處理它!」\ n \ n「'格式不正確,改爲'txt =」出錯了,處理! \ n \ n「' – javiyu 2014-09-05 15:04:38

+1

是的,它沒有被觸發,因爲你的catch中有語法錯誤 – 2014-09-05 15:05:00

回答

3

變化

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

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

How會解決一個嘗試捕捉問題嗎? – 2014-09-05 15:07:31

+0

@ pc-shooter這是一個語法錯誤 – edhedges 2014-09-05 15:08:22

+0

噢是的我看到,sry – 2014-09-05 15:08:59

相關問題