let’s say methodA and methodB will both throw a couple of types of exceptions, and some exceptions are of the same type, some are not, so what’s the best practice to handle this?
style A:
void foo()
{
try
{
methodA();
}
catch (exceptionTypeA)
{
handleA;
}
try
{
methodB();
}
catch (exceptionB)
{
handleB;
}
}
style B:
void foo()
{
try
{
methodA();
methodB();
}
catch (exceptionTypeA)
{
handleA;
}
catch (exceptionB)
{
handleB;
}
}
Neither is “better”; they’re simply different. Depending upon what you do in your
catchblock, the first option will allowmethodBto execute even ifmethodAthrows an exception of typeexceptionTypeA, while the second block requires thatmethodAsucceed in order formethodBto be called.