Why does "node. js" Use try, catch catch exception, program still crash?

Source: Internet
Author: User
Tags call back try catch

While learning "Nodejs" in the previous period, in the fourth chapter-asynchronous programming, author Pauling mentioned that one of the difficulties of asynchronous programming is exception handling, the book describes the " attempt to try/catch the asynchronous method can only capture the exception in the event loop, the call The exception thrown when the back executes will be powerless . "

Sure enough, during the project test, the node. JS process crash was encountered for two consecutive days. Through debug Log, the reason, found it is the problem mentioned in the book.

For example

1 //Test.js2 3 varTest =undefined;4 5 Try{6     varF1 =function(){7 Console.log (test.tostring ()); 8     }9 }Ten Catch(e) { OneConsole.log (' ERROR: ')); A } -  - //assume somewhere F1 () 'll be called as a call back function theF1 ();

  Here, the F1 function is a function that is passed to the calling back function to other functions and executes call back during the execution of other functions. from the surface of the code, it's easy to think that if line 7,

1 console.log (test.tostring ());

  if this line of code is an exception, it will naturally be assumed that it will be caught by a try catch and will not cause the crash of the process. But in fact, the result of the operation is:

  

  Run error, line 11 error is not printed, indicating that the error in the program was not caught by try. and Nodejs as a single-process single-threaded procedure, will cause the process of crash!

--------------------------------------------------------------------------------------------------------------- ---------

Therefore, when doing asynchronous programming, the individual feels that:

  To take into account the possible errors of the call back function, increase the type check code or add try Cach in the place where call back is actually executed, and prevent the exception from being caught causing the process to crash

--------------------------------------------------------------------------------------------------------------- ---------

As in this example, you can modify the following

1 if (typeof(TEST)! = ' undefined ') {2    console.log (test.tostring ())  ; 3 }

Or

1 console.log (Test test.tostring (): ' [undefine] ');

Or

1 Try {2    F1 (); 3 }4catch(e)5{6     console.log (' New Error: ' ); 7 }

This way, by running the program again, you can avoid the exception and avoid the crash of the process.

  

  Summary:

In node. js, there are a lot of asynchronous calling APIs, and when executing the API, when you pass the call back function, be aware of the errors that can occur in the calls back function, which can cause the process to crash if it is not caught by a normal try or otherwise avoided.

Best regards

Kevin Song

2014/6/25

  

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.