前段時間學習《深入淺出Nodejs》時,在第四章 - 異步編程中作者朴靈曾提到,異步編程的難點之一是異常處理,書中描述"嘗試對異步方法進行try/catch操作只能捕獲當次事件循環內的異常,對call back執行時拋出的異常將無能為力"。
果然,項目測試過程中,連續兩天遇到了Node.js進程Crash的問題。通過Debug Log,究其原因,發現正是書中提到的問題。
例如,
1 //test.js 2 3 var test = undefined; 4 5 try{ 6 var f1 = function(){ 7 console.log(test.toString()); 8 } 9 } 10 catch(e){ 11 console.log('error..'); 12 } 13 14 //assume somewhere f1() will be called as an call back function 15 f1();
這里模仿f1函數是做為call back(回調)函數傳遞給其他函數,在其他函數執行過程中執行call back的函數。從代碼表面來看,很容易認為如果Line 7,
1 console.log(test.toString());
如果這行code發生異常,會自然認為其會被try catch捕獲到,並不會引起進程的Crash。但其實,運行結果是:
運行錯誤,Line 11的錯誤並沒有打印,說明在程序中錯誤沒有被Try Catch。而Nodejs作為單進程單線程程序,將會引起進程的Crash!
------------------------------------------------------------------------------------------------------------------------
因此,在進行異步編程時,個人覺得:
要考慮到call back函數可能產生的錯誤,增加類型檢查代碼或在Call back被真正執行的地方增加Try cach等,避免異常未能被捕獲導致進程Crash
------------------------------------------------------------------------------------------------------------------------
如本例,可修改如下,
1 if(typeof(test) != 'undefined'){ 2 console.log(test.toString()); 3 }
或者
1 console.log(test? test.toString() : '[undefine]');
或者
1 try{ 2 f1(); 3 } 4 catch(e) 5 { 6 console.log('new error..'); 7 }
這樣,再次運行程序,就可以避免異常,避免進程的Crash。
小結:
在Node.js中,非常多的異步調用API,在執行API,傳入Call back函數時,一定要注意Call back函數里可能發生的錯誤,如果沒有被正常的Try catch到或者其他方式避免,就有可能導致進程的Crash。
Best Regards
Kevin Song
2014/6/25