I have code with this pattern - I need to call next() both when fn() is successful, or when there is an exception in fn()'s exeuction.
try {
fn.call(obj, a, b, c, function(error, next) {
...
next();
});
} catch (ex) {
// log.
next(); // This is under question.
}
The exception can happen not only in fn, but anywhere in next() also. I shouldn't be calling next() from within the catch, since I don't if the exception came from somewhere during execution of next itself.
So what approach do you folks recommend for this pattern?
Firstly I recommend you to take a look at try...catch block description (don't mind that it's for browser JS). Is it possible to place next() function inside finally block in your case? Is yes I suggest placing it there and taking the whole try...catch block inside another try...catch block.
Still throwing exception in finalization is not a good practice. Are you sure you are using try...catch in the right place? Maybe it's better to do it inside fn (or handling wxception inside error function)?