Are there any exceptions in lambdas in javascript / node.js?

I have a node.js server that I want to be able to handle exceptions without crashing, and I have some code like below. What I want to know, with all the events triggered by events and callbacks and lambdas and all that, will my exceptions still be left by my main entry point?

try { http.get(..., function(results) { // Might get an exception here results.on('data', function () { // Might also get an exception here }); results.on('end', function () { // Might also get an exception here }); }); } catch(e) { // Will the exceptions from the lambdas be caught here? console.log('Nicely caught error: (' + e.name + '): ' + e.message); } 

thanks

+8
javascript exception
source share
3 answers

It depends on the control flow. Node.js focuses on asynchrony, and one of the main disadvantages of asynchrony is that the code does not flow in such a way that you can use it with a synchronized language.

In synchronous language, the caller is blocked while the function is waiting for some data. This makes programmers work quite simply because they can be guaranteed that when a function is waiting for data to return, data will be available to the caller.

This is the exact opposite in asynchronous language or with non-blocking I / O. In this case, the caller is blocked for the duration of the function call, however, the functions should not wait for the completion of data entry or I / O before returning. This complicates the programmer a bit, because when the function call returns, there is no guarantee as to whether the data will be available. Therefore, non-blocking I / O usually involves callback functions that are called when available data is available.

try/catch blocks the work with the call stack. That is, when an exception is thrown, the runtime expands the call stack until it finds a catch that surrounds the call that caused the exception. But since http.get is a non-blocking call, it exits immediately after registering some callbacks, and processing continues. Callbacks are called in a separate "thread", and so the calls are not nested in the original try/catch .

The chart will really help explain things here, but unfortunately I do not have one available to me.

+8
source share

The error handling style for the standard node.js library should cause the same callback, but pass a nonzero first argument representing the error. If you have exceptional conditions in your asynchronous code, save it in this format.

The toss would go up the caller chain, which usually does not know what callbacks are doing (for example, at the tcp level, it doesn’t care that its data is analyzed as http). Throwable exceptions are bad for asynchronous programming.

+2
source share

In your sample code, none of the potential exceptions that have arisen from the http.get callback will fall into your catch block. The callback stack is created from the node event loop when data is available for reading.

There is a way in node to catch the thrown exceptions:

 process.on("uncaughtException", function (err) { console.log("uncaught exception: " + err); }); 

This will sort the work for your example, depending on where the exception is.

The trouble is that non-displayable exceptions can unexpectedly unravel the w630's internal operations, so you really don't want to depend on that. The only reliable way to catch all possible exceptions so that you can handle them is to try / catch every entry point from the event loop.

That sounds pretty tedious, but usually it’s not so bad. In your sample program, you use the node API for HTTP requests, which is still a very low-level interface. For most things, you would like to get this catch catch function once and use it as a library.

+2
source share

All Articles