Am I error checking too much ?

This is a discussion on Am I error checking too much ? within the C Programming forums, part of the General Programming Boards category; Hi. I'm a newb trying to learn C99. Here's some code that I wrote. My intention is to write a ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Am I error checking too much ?

    Hi. I'm a newb trying to learn C99. Here's some code that I wrote. My intention is to write a Wikipedia bot that uses libcurl. I wonder if I'm going over the top on error checking, or if I'm within acceptable boundaries.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include "Internetfunctions.h"
    #include "Error.h"
    
    #define StringMax 100
    
    static int shutdownmessage ( void )	
    {
    	int nonfatalerror = 0 ;		/* = 1 for nonfatal error, -1 for fatal error. */
    
    		/* Print shutdown message. */
    
    	int printingerror = 0 ;
    	char* p_Message = "Bot shutting down now.\n" ;
    	if ( printf ( "%s", p_Message ) != strlen ( p_Message ) )
    		printingerror = 1 ;
    
    	if ( printingerror )
    	{
    		ErrorMessage ( "Couldn't print to screen." , __FILE__ , __LINE__ );
    		nonfatalerror = 1;
    	}
    
    		/* Print shutdown time */	
    
    	struct tm *local ;
    	time_t currenttime ;
    	char Message [ StringMax ] ;
    
    	if ( time ( &currenttime ) != -1 ) 
    	{
    		local = localtime ( &currenttime ) ;
    		int x = snprintf ( Message , StringMax , "Bot shutdown at : %s" , asctime ( local ) ) ;
    		if ( printf ( "%s", Message ) != strlen ( Message ) )
    		{
    			ErrorMessage ( "Couldn't print to screen." , __FILE__ , __LINE__ );
    			nonfatalerror = 1 ;
    		}
    	}
    	else
    	{
    		ErrorMessage ( "Current local time is not available." , __FILE__ , __LINE__ );
    		nonfatalerror = 1 ;
    	}
    
    	return nonfatalerror ;
    }
    
    int shutdown ( void )
    {
    	int x = finalizeinternetfunctions () ;
    	if ( x < 0 ) return x ;
    
    	int y = shutdownmessage () ;
    	if ( y < 0 ) return y ;
    
    	if ( x > 0 ) return x ;
    	else
    	if ( y > 0 ) return y ;
    
    	return 0 ;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Richardcavell View Post
    Hi. I'm a newb trying to learn C99. Here's some code that I wrote. My intention is to write a Wikipedia bot that uses libcurl. I wonder if I'm going over the top on error checking, or if I'm within acceptable boundaries.
    You're a bit over the top... most of the print to screen and memory functions are highly trustworthy. Besides... if printing to the screen is not working, how do you plan to display the error message?

    The stuff you need to be checking are the I/O functions where "crap happens" far more often; networking, file I/O, serial comms, memory allocations etc.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include "Internetfunctions.h"
    #include "Error.h"
    
    #define StringMax 100
    
    static int shutdownmessage ( void )	
    {
    //	int nonfatalerror = 0 ;		/* = 1 for nonfatal error, -1 for fatal error. */
    
    		/* Print shutdown message. */
    
    //	int printingerror = 0 ;
    //	char* p_Message = "Bot shutting down now.\n" ;
    //	if ( printf ( "%s", p_Message ) != strlen ( p_Message ) )
    //		printingerror = 1 ;
    
    printf("Bot Shutting down now.\n");
    
    //	if ( printingerror )
    //	{
    //		ErrorMessage ( "Couldn't print to screen." , __FILE__ , __LINE__ );
    //		nonfatalerror = 1;
    //	}
    
    // NOTE: Printing __LINE__ only works with debug code in your files. 
    // release versions don't know line numbers.
    
    
    		/* Print shutdown time */	
    
    	struct tm *local ;
    	time_t currenttime ;
    	char Message [ StringMax ] ;
    
    	if ( time ( &currenttime ) != -1 ) 
    	{
    		local = localtime ( &currenttime ) ;
    //		int x = snprintf ( Message , StringMax , "Bot shutdown at : %s" , asctime ( local ) ) ;
    
    printf("Bot Shutdown at : %s" asctime(local));
    
    //		if ( printf ( "%s", Message ) != strlen ( Message ) )
    //		{
    //			ErrorMessage ( "Couldn't print to screen." , __FILE__ , __LINE__ );
    //			nonfatalerror = 1 ;
    //		}
    //	}
    //	else
    //	{
    //		ErrorMessage ( "Current local time is not available." , __FILE__ , __LINE__ );
    //		nonfatalerror = 1 ;
    //	}
    
    //	return nonfatalerror ;
    }
    
    int shutdown ( void )
    {
    shutdownmessage(); 
    
    	int x = finalizeinternetfunctions () ;
    	if ( x < 0 ) 
                      return x ;
      
    
    //	int y = shutdownmessage () ;
    //	if ( y < 0 ) return y ;
    
    //	if ( x > 0 ) return x ;
    //	else
    //	if ( y > 0 ) return y ;
    
    	return 0 ;
    }

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Yeah, you're error checking too much.

    Tater, if he can't print to the screen, he could return a specific integer, like 69, to note where and when the error happened. He could just do an echo $? to see his return value.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    printf can fail say if your program is invoked as
    Code:
      your_program > outfile.txt
    Say no disk space or something went wrong. try to simulate what happened. say write to almost full usb drive more that what it can hold.
    But yes it's too much for your case.
    Last edited by Bayint Naung; 02-26-2011 at 10:01 AM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


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