Consider the following design pattern:
do {
// Some logic here
if (AnErrorOccurs()) {
break;
}
// Some more logic here
if (ADifferentErrorOccurs()) {
break;
}
// Code completed successfully
return someValue;
} while (false);
// Lengthy error-handling code here
return errorCode;
Is using a loop in this way appropriate? I simply want the functionality of the break statement so that I can put my error handling code in one place. A separate method would work too, true, but suppose there's a large number of variables in scope that would be unwieldy or unsafe to pass around as parameters.
Or would the try-finally pattern be a better way of handling this situation? Or throwing a whole bunch of custom exceptions? I'm just interested to see how others handle it in the interest of having the neatest and most maintainable code.
Aucun commentaire:
Enregistrer un commentaire