how much should i handle errors ?

This is a discussion on how much should i handle errors ? within the C Programming forums, part of the General Programming Boards category; hello since i work a lot with file i thouth adding this block to a project but im not sure ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    112

    how much should i handle errors ?

    hello since i work a lot with file i thouth adding this block to a project but im not sure if to do so :

    Code:
    FILE* file_accses(char *path_to_file , char* string)
    {
    	FILE* fp = NULL;
    	fp = fopen(path_to_file , string);
    	if (fp == NULL)
    	{
    		switch (errno)
    		{
    			
    			case EINTR:
        
    			{
    				printf("A signal was caught during fopen(). \n");
    				break;
    			}
     			case EISDIR:
    			{
    				printf("The named file is a directory and mode requires write access. \n");
    				break;
    			}
    			case ELOOP:
    			{
    				printf(" Too many symbolic links were encountered in resolving path. \n");
    				break;
    			}
    
    			case ENAMETOOLONG:
    			{	
    				printf("Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}. \n");
    				printf("The length of the filename exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}. \n");
    				break;
    			}
    			case ENFILE:
    			{
     				printf("The maximum allowable number of files is currently open in the system. \n");
    				break;
    			}
    			case ENOENT:
    			{
    				printf("A component of filename does not name an existing file or filename is an empty string. \n");
    				break;
    			}
    			case ENOSPC:
    			{
    				printf("The directory or file system that would contain the new file cannot be expanded, the file does not exist, and it was to be created. \n");
    				break;
    			}
    			case ENOTDIR:
    			{
        			printf("A component of the path prefix is not a directory. \n");
    				break;
    			}
    			case ENXIO:
    			{
        			printf("The named file is a character special or block special file, and the device associated with this special file does not exist. \n");
    				break;
    			}
    			case EOVERFLOW:
    			{
    				printf("The named file is a regular file and the size of the file cannot be represented correctly in an object of type off_t. \n");
    				break;
    
    			}	
    			case EROFS:
    			{		
        			printf("The named file resides on a read-only file system and mode requires write access. \n ");
    				break;
    			}	
    			case EINVAL:
    			{
        			printf("The value of the mode argument is not valid. \n");
    				break;
    			}
    			case EMFILE:
    			{
        			printf("{FOPEN_MAX} streams are currently open in the calling process. \n");
    				printf("{STREAM_MAX} streams are currently open in the calling process. \n");
    				break;
    			}
    			case ENOMEM:
    			{
    				printf("Insufficient storage space is available. \n");
    				break;
    			}
    			case ETXTBSY:
    			{
        			printf("The file is a pure proced\n");
    				break;
    			}	
    	
    		}
    	
    	}
    	return fp;
    }
    maybe it's too much ?
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I'd suggest putting the switch in a file error handling function.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    will it be wise to collect all eror handling function in a seprate header + c file ?
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    sure, I think.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to think about using strerror() instead of your own version of error text, since that will:
    a) be consistant with what everyone else is saying about the same form of error.
    b) you won't have a problem that if you change the OS version, there's a new error that you don't know about.

    At the very least, add a "default: printf(strerror(errno));", so that if there is an error you didn't cover, there's some sort of error message to explain what went wrong.

    This of course applies to ALL forms of errors that occur.

    And I agree that if you do feel that it's necessary to detail the error message for every error-code that could possibly come out of the system with your own wording, then you should put all of that code in a separate function in a separate file.

    Also if you have a long string that doesn't fit on one line, you can just break it like this:
    Code:
       printf("Some very long long long long\n"
                "text that continues on another line");
    It is completely unneccessary to call printf twice, just because the string is long (as long as it's shorter than the C-compilers standard limit, which is probably about half a page-full or so). [I have seen the error message from Microsoft C, but it was a while back, and it was actually something wrong with that string]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. a simple C question...
    By DramaKing in forum C Programming
    Replies: 10
    Last Post: 07-28-2002, 02:04 PM
  3. API Reading Files, handle is always -1
    By Xei in forum C++ Programming
    Replies: 13
    Last Post: 05-06-2002, 10:16 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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