Exception Handling in C
errno is a global variable indicating the error occurred during any function call and it is defined inside <errno.h> header file.
When a function is called in C, a variable named errno is automatically assigned a code (value) which can be used to identify the type of error that has been encountered. Different codes (values) for errno mean different types of errors.
// C program to check and rectify
// divide by zero condition
#include <stdio.h>
#include <stdlib.h>
void function(int);
int main()
{
int x = 0;
function(x);
return 0;
}
void function(int x)
{
float fx;
if (x == 0) {
printf("Error: Division by Zero Error\n");
exit(EXIT_FAILURE);
}
else {
fx = 10 / x;
printf("f(x) is: %.5f", fx);
}
}
Output
Error: Division by Zero Error
Exception Handling in Programming
Exception handling is a critical aspect of programming, enabling developers to manage unexpected or erroneous situations gracefully. In this article, we’ll discuss the concept of exception handling, its importance, and best practices for implementing it effectively in various programming languages.
Table of Content
- What is Exception Handling?
- Components of Exception Handling
- Exception Handling in C
- Exception Handling in C++
- Exception Handling in Java
- Exception Handling in Python
- Exception Handling in C#
- Exception Handling in JavaScript
- Exception Handling Best Practices
Contact Us