node js catch error code example
Example 1: node js try catch
try {
nonExistentFunction();
} catch (error) {
console.error(error);
}
Example 2: node js throw error
FactoryController.prototype.create = function (callback) {
throw new Error('An error occurred');
try {
this.check(function (check_result) {
callback(check_result);
});
} catch (ex) {
throw new Error(ex.toString());
}
}
FactoryController.prototype.create = function (callback) {
try {
this.check(function (check_result) {
throw new Error('An error occurred');
});
} catch (ex) {
throw new Error(ex.toString());
}
}
Example 3: node js catch any errors
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});