B
barcaroller
I am trying to adopt a model for calling functions and checking their return
values. I'm following Scott Meyer's recommendation of not over-using
exceptions because of their potential overhead.
Here's the approach I'm currently looking at. I throw exceptions only from
constructors. Destructors, of course, do not throw exceptions. All other
functions return a signed integer. The values are all stored in one large
header file (as 'defines' or 'enums').
* If the return value == 0, the operation was successful and the caller
does not have take any action.
* If the return value > 0, the operation was successful but the callee is
trying to tell the caller something and the caller may or may not take
action based on the return value.
* If the return value < 0, the operation was not successful and the caller
needs to handle the error accordingly.
Example:
void caller()
{
RetVal_t retval = createFile();
if (retval >= 0)
{
//
// Success; file was created
// We could return here or continue to probe
//
switch (retval)
{
case 0:
// file was created; no additional information
...
case FILE_CREATED_READ_ONLY:
// file was created but it is read-only
...
}
return;
}
if (retval < 0)
{
//
// We have run into an error that must be handled
//
switch (retval)
{
case DIRECTORY_DOES_NOT_EXIST:
...
case PERMISSION_DENIED:
...
case FILE_ALREADY_EXISTS:
...
}
return;
}
}
Does anyone see problems with this approach? Are there better ways to
handle function return values (especially with deeply nested functions)?
values. I'm following Scott Meyer's recommendation of not over-using
exceptions because of their potential overhead.
Here's the approach I'm currently looking at. I throw exceptions only from
constructors. Destructors, of course, do not throw exceptions. All other
functions return a signed integer. The values are all stored in one large
header file (as 'defines' or 'enums').
* If the return value == 0, the operation was successful and the caller
does not have take any action.
* If the return value > 0, the operation was successful but the callee is
trying to tell the caller something and the caller may or may not take
action based on the return value.
* If the return value < 0, the operation was not successful and the caller
needs to handle the error accordingly.
Example:
void caller()
{
RetVal_t retval = createFile();
if (retval >= 0)
{
//
// Success; file was created
// We could return here or continue to probe
//
switch (retval)
{
case 0:
// file was created; no additional information
...
case FILE_CREATED_READ_ONLY:
// file was created but it is read-only
...
}
return;
}
if (retval < 0)
{
//
// We have run into an error that must be handled
//
switch (retval)
{
case DIRECTORY_DOES_NOT_EXIST:
...
case PERMISSION_DENIED:
...
case FILE_ALREADY_EXISTS:
...
}
return;
}
}
Does anyone see problems with this approach? Are there better ways to
handle function return values (especially with deeply nested functions)?