Async error handling

suggest change

Try catch

Errors must always be handled. If you are using synchronous programming you could use a try catch. But this does not work if you work asynchronous! Example:

try {
    setTimeout(function() {
        throw new Error("I'm an uncaught error and will stop the server!");
    }, 100); 
}
catch (ex) {
    console.error("This error will not be work in an asynchronous situation: " + ex);
}

Async errors will only be handled inside the callback function!

Working possibilities

Event handlers

The first versions of Node.JS got an event handler.

process.on("UncaughtException", function(err, data) { 
    if (err) {
        // error handling
    }
});

Domains

Inside a domain, the errors are release via the event emitters. By using this are all errors, timers, callback methodes implicitly only registrated inside the domain. By an error, be an error event send and didn’t crash the application.

var domain = require("domain");
var d1 = domain.create();
var d2 = domain.create();

d1.run(function() {
    d2.add(setTimeout(function() {
        throw new Error("error on the timer of domain 2");
    }, 0));
});

d1.on("error", function(err) {
    console.log("error at domain 1: " + err);
});

d2.on("error", function(err) {
    console.log("error at domain 2: " + err);
});

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents