This document explains how to handle errors in C programming using techniques like errno, perror(), and exit(). Learn how to manage runtime errors, file errors, and system errors with practical examples. Ideal for second-year Computer Science students, this guide will help you improve the reliabili...
Error Handling in C
Error handling in C is an essential concept to ensure robust and reliable programs.
Unlike some modern programming languages, C does not provide built-in support
for error handling, such as exceptions. Instead, error handling in C is implemented
using traditional mechanisms like return values, error codes, and global variables.
1. Using Return Values for Error Handling
Functions in C often return a specific value to indicate success or failure. This
value can be checked to handle errors appropriately.
Example: Checking Return Values
#include <stdio.h>
int divide(int a, int b, int* result) {
if (b == 0) {
return -1; // Return an error code for division by zero
}
*result = a / b;
return 0; // Success
}
int main() {
int a = 10, b = 0, result;
if (divide(a, b, &result) != 0) {
printf("Error: Division by zero\n");
} else {
printf("Result: %d\n", result);
}
return 0;
}
, In this example, the divide function returns -1 to indicate an error (division by
zero), and the caller checks the return value.
2. Global Variable errno
The <errno.h> header file provides a global variable errno that stores error codes
set by certain library functions. These codes indicate the type of error
encountered.
Common Error Codes
EIO: Input/output error
ENOMEM: Out of memory
EINVAL: Invalid argument
EBADF: Bad file descriptor
int main() {
FILE* file = fopen("nonexistent.txt", "r");
if (file == NULL) {
printf("Error: %s\n", strerror(errno)); // strerror() provides a readable error
message
}
return 0;
}
In this example, attempting to open a nonexistent file sets errno. The strerror()
function converts the error code to a human-readable message.
3. Custom Error Codes
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller rileyclover179. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.89. You're not tied to anything after your purchase.