Thread: What will be considered better practice; error handling.

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    What will be considered better practice; error handling.

    Hi.
    I was asked to write, as a class exercise, a program that gets as parameter two files paths and determines whether or not these two files are identical. it should return 1 if they are, and 0 if they're not.
    So I wrote a function, and it works properly, I'm just not sure how to handle the errors;
    The function makes some system calls (open()/read()...), now, what is the right way of the two to handle errors that might be caused by the system calls, should I:
    1. Call perror() from within the function to notify the user about the error.
    2. Return -1 from the function to main(), and let main() makes the call for perror.
    On one hand, calling perror() from within the function can be more informative, on the other hand, I'm almost sure that functions do not suppose to print anything, and that it's considered bad practice.

    Thanks.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    It depends on what errors you are going to be dealing with

    For an open sort of error, it is usually best to inform the user that the file can't open and help them try and recover the situation as part of the code that opens the file ("Can't open file. Check to see if the name is correct and is not protected"... (R)etry/(C)ancel). What you don't want is your user to loose work because the file did not open -> Keep retrying until the file is opened or the user has cancelled the open.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Gotos for good error handling practice?
    By 127.0.0.1 in forum C Programming
    Replies: 5
    Last Post: 04-28-2011, 01:21 PM
  2. Cascading bool returns, bad error handling practice?
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2007, 03:38 AM
  3. Is it considered rude...
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-17-2005, 11:42 AM
  4. Considered bad design?
    By Eibro in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 08:56 PM
  5. Is this considered an AND operation?
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-22-2002, 02:40 PM