In this javascript code f.init() control the entire flow of the program:
var f = function() {
return {
init: function(var1) {
try {
f.f1(1)
} catch (e) {
alert(e);
}
try {
f.f2(1)
} catch (e) {
alert(e);
}
// ...
// more try-catch blocks
// ...
},
f1: function() {
throw Error('f1 called');
},
f2: function() {
throw Error('f2 called');
}
};
}();
f.init();
How I could centralize all exceptions management in only one try-catch block? Something like this:
var f = function() {
return {
init: function(var1) {
f.f1(1) // this cut the control flow if thrown some error
f.f2(1) // so this is not called
},
f1: function() {
throw Error('f1 called');
},
f2: function() {
throw Error('f2 called');
}
};
}();
try {
f.init();
} catch (e) {
alert(e);
}
The previous code cut the flow after thrown some error.
You can’t. Once an error is thrown, the program flow breaks.