Last updated : July 27, 2026

Error Handling Tutorials

Programming

Error Handling in C

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.


Why Do We Need Error Handling?

Error handling helps to:

  • Prevent program crashes.

  • Detect invalid operations.

  • Handle file-related errors.

  • Detect memory allocation failures.

  • Improve program reliability and debugging.


What is errno?

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.

Example

plaintext

#include <errno.h>
#include <stdio.h>

int main()
{
    FILE *fp;

    fp = fopen("gfg.txt", "r");

    printf("errno = %d", errno);

    return 0;
}

Output

plaintext

errno = 2

Common errno Values

  • 1 → Operation not permitted

  • 2 → No such file or directory

  • 5 → I/O error

  • 12 → Out of memory

  • 13 → Permission denied


Methods of Error Handling in C

C provides several ways to handle errors.

1. Using if-else

The simplest method is checking return values using if-else.

Example

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;
}

Output

plaintext

File opening error

2. perror()

perror() prints a descriptive error message based on the current value of errno.

Example

plaintext

#include <stdio.h>

int main()
{
    FILE *fp = fopen("gfg.txt", "r");

    perror("Error");

    return 0;
}

Output

plaintext

Error: No such file or directory

3. strerror()

strerror() returns the error message as a string.

Example

plaintext

#include <errno.h>
#include <stdio.h>
#include <string.h>

int main()
{
    FILE *fp = fopen("gfg.txt", "r");

    printf("%s", strerror(errno));

    return 0;
}

Output

plaintext

No such file or directory

4. ferror()

ferror() checks whether an error occurred during a file operation.

Example

plaintext

if(ferror(fp))
{
    printf("Error occurred");
}

5. feof()

feof() checks whether the end of a file has been reached.

Example

plaintext

if(feof(fp))
{
    printf("End of file reached");
}

6. clearerr()

clearerr() clears both EOF and error flags of a file stream.

Example

plaintext

clearerr(fp);

Exit Status

The exit() function terminates a program and returns a status code to the operating system.

Constants

  • EXIT_SUCCESS

  • EXIT_FAILURE

Example

plaintext

#include <stdlib.h>

int main()
{
    exit(EXIT_SUCCESS);
}

Manual Error Handling

Some errors can be handled manually using conditions.

Division by Zero

Example

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;
}

Output

plaintext

Error: Division by zero is not allowed

Best Practices

  • Always 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.


Summary

  • 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

Job PortalJobs