Error Handling In C

Error Handling - is the process to provide proper messaging to user if application does not responding as expected. For example - you are opening a file and file does not exists then system will generate system error and end user would not be able to understand what happened so this is the the programmer's responsibility to handle such type of errors possibility in application and will show meaningful message to user.    

C does not provide direct way to handle errors - what does it mean ?  

Let's understand - other languages like C++ handle errors directly i.e. it tells programmer to keep your code within the block called "try" and in case of error your error will be handle by another block itself called "catch" but C language does not support direct feature for error handling. 

C generates error codes, error codes are defined in <errno.h> header file. C provides global variable called "errno", it contains error no generated by system. Its default value is 0

you can change its value to generate other errors.

Errors can be defined in two types 1. Compile-Time Error 2. Run-Time Error. 

Compile-Time Error - 

This error checked by compiler while writing code. Compiler checks all syntax errors and highlights with red underline, it indicates that such errors has to be removed before compilation process. It saves programmer's time.These errors are reported by compiler that why it is known as compile-time errors.

#include <stdio.h>

int main() {

// ; is missing
int a =6 

printf( "The value of a is %d",a);

// incorrect spelling of return
retun 0;

}


Run-Time Error -

This error occurs while running the program,it reports invalid operations. C generates error codes for run-time errors. See some error codes given below.    
       
Error Codes
Meaning
1
Operation Not Permitted
2
No Such File Or Directory
3
No Such Process
4
Interrupted System Call
5
Input/Output Error
6
No Such Device Or Address
7
Argument List Too Long
8
Exec Format Error
9
Bad File Number
10
No Child Process
11
Try Again

12
Out Of Memory
13
Permission Denied

Program 1 : 
Write a program to open any existing file and check its status.


#include <stdio.h>

#include <errno.h>


//Error No - 2 : No Such File/Directory Example

int main() {


//File pointer
FILE *ptr;

char fileName[50];


printf("Enter file path to open");

scanf("%s",fileName);


// Open file in read mode
fopen(fileName,"r");


// checking file open status 
if(errno == 2) {

  
  printf("File is not available, Please check the path");
  
}

return 0;
}


In this example you can see, we are going to open existing file, if file is not available then system will generate error code "2", programmer checks error and write appropriate message to user. 

"C" provides some other methods perror() , strerror   both has its own purpose let's understand with example.

perror()

This method displays system generated error message , programmer can add own message or leave it as blank.

strerror()

This method displays system generated message of specific error no.

Program 2 : 
Write a program to show the use of perror () and strerror() methods in C.

#include <stdio.h>

#include <errno.h>


int main(void) {


FILE *f ;


f = fopen("c:\\data.txt","r+");


if(f == NULL) {


perror("System Generated Error - ");// or make it empty perror("");


// displaying error no
printf("Error No is - %d\n",errno);


//message according to error no.
printf("%s\n", strerror(errno));

}
  return 0;
}

Logical Error -  

As the name suggest, it occurs when programmer made some logical mistakes in source code that result the incorrect result or unexpected behavior. Logical errors are difficult to find out programmer have to check each condition or possibilities. 

For example running while loop with invalid condition that will never meet so that program can  crash. 

Another example we have to count some values but counter is initialized by 1, in this case incorrect output will come.

Program 3 : 
Write a program to get count of capital letters in string.

#include <stdio.h>

#include <string>


int main(void) {


char string[10];

int count =1; //intialized by 1, result will be incorrect


printf("Enter any string");

scanf("%s",string);


for(int i=0;i<strlen(string);i++) { //strlen to get length of string


   if(string[i] >='A' && string[i] <='Z' )

         
         count ++; 

}

printf("Count of Capital letter in string is - %d",count);


  return 0;

}

OUTPUT -

Enter any string - TeST

Count of Capital letter in string is - 4
Program 4 : 
Write a program to print 1 to 10 numbers using while loop.

#include <stdio.h>


void main() {


int i = 1;


while(i<=10) {


  printf("%d\n", i);

  // i++ is missing,will cause unexpected behavior


}
  
}


Thanks for reading, please do share and comments your suggestions.

No comments

Powered by Blogger.