I'm working on Nodejs service-side application, my case is that i want to return the throw an exception to the caller(who did call the function), i did two cases, one outside the callback, and the another inside the callback, also the parent has try catch blocks.
concept : throw(Business function)-->throw (Business function) -->try & catch
Outside the callback is working correctly. Inside the callback didn't return the exception to the parent.
I want to to this scenario,because I'm looking to throw an exception to the parent and stop completion the functions, and this's exist in Java, C++, C and .NET.
So why this scenario is not working with me ?!
My example here with 2 different cases :
FactoryController.prototype.create = function (callback) {
//The throw is working, and the exception is returned.
throw new Error('An error occurred'); //outside callback
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) {
//The throw is not working on this case to return the exception to the caller(parent)
throw new Error('An error occurred'); //inside callback
});
} catch (ex) {
throw new Error(ex.toString());
}
}