Originally Posted by
brewbuck
Exception propagation is itself an execution path. Just
using exceptions instead of return codes doesn't relieve you of the task of
testing the error paths.
Lets use an example:
Code:
int displayImage()
{
int err = loadImage(...);
if(-5 == err)// not enough memory. can't be handled here
{
return ...;
}
err = blitImage(...);
if(-28 == err)// init display system failed. cant be handled here
{
return ...;
}
[...]
}
int main()
{
int err = displayImage(...);
if()// out of mem
if()//display subsystem failed
}
and using execptions
Code:
void displayImage()
{
loadImage(...); // may throw out of mem
blitImage(...); // may throw init failure
[...]
}
int main()
{
try
{
displayImage(...);
}
catch(const out_of_mem&)
{
//free some and retry
}
catch(const init_failure&)
{
// reset something and retry
}
}
Ok, the number of execution paths seems to be the same for both variants so my statement about it was wrong. But the first one needs a lot of conditions which are error prone to write. This could be a point for exception handling