Stack unwinding is different from simply returning. It also involves a search for an error handler (a catch block) in each lower level in the stack. That's what makes it a heavy process.
That's why you should only use exceptions for truly exceptional circumstances. The warnings about exception handling are for those folks who simply see an exception as a way to deliver data higher up in the stack; folks who like to do "clever" programming. They think it's a clever way to get around a problem, but they instead create two new problems they hadn't anticipated.
In general, you're best off using exceptions (for truly exceptional circumstances) rather than return codes, as this makes your code easier to read and maintain. For example, which is easier to read and maintain?
void MyMethod()
{
try
{
Method1();
Method2();
Method3();
}
catch(SomeException const & e) // edited per Mordachai's suggestion
{
// handle SomeException
}
catch(SomeOtherException const & e)
{
// handle SomeOtherException
}
}
void MyMethod()
{
int err;
err = Method1();
switch(err)
{
case SOMEERRORCODE:
// handle some error code
break;
case SOMEOTHERERRORCODE:
// handle some other error code
break;
}
err = Method2();
switch(err)
{
case SOMEERRORCODE:
// handle some error code
break;
case SOMEOTHERERRORCODE:
// handle some other error code
break;
}
err = Method3();
switch(err)
{
case SOMEERRORCODE:
// handle some error code
break;
case SOMEOTHERERRORCODE:
// handle some other error code
break;
}
}