Loading content...
Loading content...
Error handling is the process of detecting and managing errors that occur during program execution.
Unlike Java and Python, C does not provide built-in exception handling (try-catch). Instead, C uses return values, error codes, and library functions to detect and handle errors.
Error handling helps to:
Prevent program crashes.
Detect invalid operations.
Handle file-related errors.
Detect memory allocation failures.
Improve program reliability and debugging.
errno is a global variable defined in the <errno.h> header file.
When a function fails, the system automatically sets errno to a specific error code that identifies the reason for the failure.
plaintext
#include <errno.h>
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("gfg.txt", "r");
printf("errno = %d", errno);
return 0;
}plaintext
errno = 21 → Operation not permitted
2 → No such file or directory
5 → I/O error
12 → Out of memory
13 → Permission denied
C provides several ways to handle errors.
The simplest method is checking return values using if-else.
plaintext
#include <stdio.h>
int main()
{
FILE *fp = fopen("gfg.txt", "r");
if(fp == NULL)
{
printf("File opening error");
}
else
{
printf("File opened successfully");
}
return 0;
}plaintext
File opening errorperror() prints a descriptive error message based on the current value of errno.
plaintext
#include <stdio.h>
int main()
{
FILE *fp = fopen("gfg.txt", "r");
perror("Error");
return 0;
}plaintext
Error: No such file or directorystrerror() returns the error message as a string.
plaintext
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp = fopen("gfg.txt", "r");
printf("%s", strerror(errno));
return 0;
}plaintext
No such file or directoryferror() checks whether an error occurred during a file operation.
plaintext
if(ferror(fp))
{
printf("Error occurred");
}feof() checks whether the end of a file has been reached.
plaintext
if(feof(fp))
{
printf("End of file reached");
}clearerr() clears both EOF and error flags of a file stream.
plaintext
clearerr(fp);The exit() function terminates a program and returns a status code to the operating system.
EXIT_SUCCESS
EXIT_FAILURE
plaintext
#include <stdlib.h>
int main()
{
exit(EXIT_SUCCESS);
}Some errors can be handled manually using conditions.
plaintext
#include <stdio.h>
int main()
{
int a = 10;
int b = 0;
if(b == 0)
{
printf("Error: Division by zero is not allowed");
}
else
{
printf("%d", a / b);
}
return 0;
}plaintext
Error: Division by zero is not allowedAlways check file pointers after fopen().
Check memory allocation after malloc() and calloc().
Use perror() and strerror() for debugging.
Free dynamically allocated memory using free().
Validate user input before processing.
C does not provide exception handling like Java.
Error handling is mainly done using return values and error codes.
errno stores error information.
perror() and strerror() display error descriptions.
ferror(), feof(), and clearerr() are useful for file handling.
Always validate operations to prevent runtime errors