Checking return values versus function handling errors...

This is a discussion on Checking return values versus function handling errors... within the C Programming forums, part of the General Programming Boards category; I have a bit of a style question. Which is more widely used? Having the calling function check the return ...

  1. #1
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179

    Checking return values versus function handling errors...

    I have a bit of a style question. Which is more widely used?

    Having the calling function check the return value of a function and handle any errors.. or having the called function handle errors? Whose responsibility should this be? I am partial to having the caller check for errors.

    For example:
    Caller checks return value of function and handles error:
    Code:
    if (CheckArgs(argc, 2) == 1){
                    fprintf(stderr, "Program only accepts 2 arguments... aborting.");
                    return 1;}
    I prefer that opposed to having the CheckArgs() function check and handle the error. Is this more standard? It appears standard because nearly every system function uses this kind of error checking.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would imagine that in most situations, the called function can't handle the error -- usually you want to give the calling program a chance to salvage what it can. And unless you're passing the entire state of your program into this function, the called function can't do that.

    An exception would be for something that isn't an error, or at least isn't an unexpected error -- user providing bad data. I would expect a GetFileToOpen function to ask the user again if the first filename given was invalid, and only return an error if the user canceled the process.

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Thanks, I have been trying to design better.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    For file errors, an error message generating function can check because of the global variable errno (#include <errno.h>). I use something like this:
    Code:
    void check_cause(void) {
    printf("Cause: ");
    switch (errno) {
    case ENOENT:
    	printf("No such file or directory.\n");
    	break;
    case EACCES:
    	printf("Permission denied.\n");
    	break;
    case EINVAL:
    	printf("Invalid argument.\n");
    	break;
    case EMFILE:
    	printf("Too many open files. Now at %d\n", num_distribute);
    	break;
    default:
    	printf("Error Code %d\n", errno);
    	break;
    	}
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,530
    Or just use the strerror() function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  3. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  4. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21