異常處理最常見的方式像下面這樣:
function throwException() {
throw new Error('Problem!');
}
try {
throwException();
} catch (e) {
console.log(e); // 錯誤:信息
console.log(e.stack); // 非標準,但大部分瀏覽器支持
}
try分支包裹易出錯的代碼,如果try分支內(nèi)部拋出異常,catch分支將會執(zhí)行。
更多建議: