Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Function return...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 6 Topic 44646 of 48417
Post > Topic >>

Function return values...

by "barcaroller" <barcaroller@[EMAIL PROTECTED] > Apr 10, 2008 at 10:02 PM

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)?
 




 6 Posts in Topic:
Function return values...
"barcaroller" &  2008-04-10 22:02:15 
Re: Function return values...
Looney <hardy_melbourn  2008-04-10 21:18:11 
Re: Function return values...
Saeed Amrollahi <s_amr  2008-04-10 23:30:41 
Re: Function return values...
peter koch <peter.koch  2008-04-11 11:12:52 
Re: Function return values...
James Kanze <james.kan  2008-04-12 01:21:38 
Re: Function return values...
"barcaroller" &  2008-04-12 18:01:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 2:48:17 CST 2008.