What Is Error Handling?

The treatment of errors in cat is not ideal. The trouble is that if one of the files can’t be
accessed for a few reason, the diagnostic is printed at the top of the concatenated output. That might be acceptable if the output goes to a screen, but not if it’s going into a file or into another program via a pipeline.

To handle this example better, a second output stream, called stderr, is assigned to a
program in the same way that stdin and stdout are.

Output written on stderr normally appears on the screen albeit the quality output is redirected.Let us revise cat to write down its error messages on the quality error.

Example Of Error Handling Program

#include<stdio.h>
/* cat: concatenate files, version 2 */
main(int argc, char *argv[])
{
FILE *fp;
void filecopy(FILE *, FILE *);
char *prog = argv[0]; /* program name for errors */
 if (argc == 1 ) /* no args; copy standard input */ 
filecopy(stdin, stdout); 
else while (--argc > 0)
 if ((fp = fopen(++argv, "r")) == NULL) {
fprintf(stderr, "%s: can't open %sn",
prog, *argv);
exit(1);
} else {
filecopy(fp, stdout);
fclose(fp);
}
if (ferror(stdout)) {
fprintf(stderr, "%s: error writing stdoutn", prog);
exit(2);
}
exit(0);
}

The program signals errors in two ways. First, the diagnostic output produced by fprintf goes to stderr, so it finds its thanks to the screen rather than disappearing down a pipeline or into an output file.

We included the program name, from argv[0], within the message, so if this program is used with others, the source of an error is identified.

Second, the program uses the quality library function exit, which terminates program
execution when it is called.

The argument of exit is out there to whatever process called this one, therefore the success or failure of the program are often tested by another program that uses this one as a sub-process.

Conventionally, a return value of 0 signals that each one is well; non-zero. values usually signal abnormal situations. exit calls fclose for every open computer file , to
flush out any buffered output.

Within main, return expr is equivalent to exit(expr). exit has the advantage that it can be called from other functions, which calls thereto are often found with a pattern-searching program .

The function ferror returns non-zero if an error occurred on the stream fp.

int ferror(FILE *fp)

Although output errors are rare, they are doing occur (for example, if a disk fills up), so a production program should check this as well.

The function feof(FILE *) is analogous to ferror; it returns non-zero if end of file has
occurred on the specified file.

int feof(FILE *fp)

We have generally not worried about exit status in our small illustrative programs, but any
serious program should lookout to return sensible, useful status values.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Error Handling In C

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Explore Tutorials By Categories

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password