throw new Error()記錄錯誤并停止正在運行的檔案,對嗎?我想做一些類似于拋出錯誤的事情,但我希望檔案繼續運行,就像你做普通的 console.log 一樣。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
你可以使用一個try...catch陳述句。如果 try 塊中的代碼拋出例外,則 catch 塊中的代碼將被執行。然后繼續執行。
let somethingElse = function() {
alert('something else')
}
try {
nonExistentFunction();
} catch (error) {
console.error(error);
// expected output: ReferenceError: nonExistentFunction is not defined
// Note - error messages will vary depending on browser
somethingElse()
}
console.log('after')
uj5u.com熱心網友回復:
您必須將整個 main 函式包裝在 try catch 塊中:
function main() { ... }
try {
main();
} catch (error) {
console.error('There was a critical error while running the app');
console.error(error);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494664.html
標籤:javascript 节点.js 控制台日志
