I've got the following:
var http = require('http');
server = http.createServer(function(request, response) {
try {
// Register a callback: this happens on a new stack on some later tick
request.on('end', function() {
throw new Error; // Not caught
})
throw new Error; // Caught below
}
catch (e) {
// Handle logic
}
}
Now, the first Error gets caught in the try...catch, but the second Error does not appear to be getting caught.
A couple questions:
Error not get caught because it occurs on a different stack? If so, am I to understand that try...catch behavior is not lexically bound, but depends instead on the current stack? Am I interpreting this correctly?You could also use a simple EventEmitter to handle an error in a case like this.
var http = require('http');
var EventEmitter = require("events").EventEmitter;
server = http.createServer(function(request, response) {
var errorHandler = new EventEmitter;
// Register a callback: this happens on a new stack on some later tick
request.on('end', function() {
if(someCondition) {
errorHandler.emit("error", new Error("Something went terribly wrong"));
}
});
errorHandler.on("error", function(err) {
// tell me what the trouble is....
});
});
The second error is never thrown, nor caught. You throw an error before you add your anonymous function as a handler for request's end event.
Even if you had, it isn't going to be caught, as it is indeed on a different stack. You must raise an error event if you want to work this way, or simply pass error as the first callback parameter to any callback function. (These are both common conventions, not mandates.)
catch catches Errors that are thrown while executing the content of its try block. Your 'end' handler is created and assigned inside the try block, but it obviously doesn't execute there. It executes when request fires its end event, and the try block will be long forgotten by then.
You'll generally want to catch errors within your event handler and not let them percolate up through the stack. You'll never/rarely want to throw them.
As a last resort, you can register an handler for 'uncaughtException' on process, which will catch all exceptions that aren't caught elsewhere. But that's rarely the right solution. http://nodejs.org/api/process.html#process_event_uncaughtexception