所以在我的應用程序發生特定情況時,我想顯示一個警報,然後停止程序執行。我在某處讀到這可以用throw()完成,但我無法完成這項工作。在錯誤情況下停止JavaScript程序操作?
這是我已經試過:
function check_for_error(data) {
try {
if (<error condition>) {
throw "error";
}
} catch(e) {
alert('error occured');
// I want program execution to halt here but it does not,
// it continues within the calling code
}
}
您可以嘗試從catch塊中拋出錯誤,然後在callee方法中處理它。基本上退出。 – Sid 2011-12-16 19:10:45