Error Handling Low Level Programming Exception Handling errnoperrorstrerror

  • Slides: 14
Download presentation
Error Handling ● Low Level Programming ● Exception Handling ● errno/perror/strerror ● User defined

Error Handling ● Low Level Programming ● Exception Handling ● errno/perror/strerror ● User defined error Md. Jakaria Lecturer Dept. of CSE, MIST

Divide by zero What is the output of the following program?

Divide by zero What is the output of the following program?

Divide by zero What is the output of the following program?

Divide by zero What is the output of the following program?

Error or Exception o A programs that work when everything is expected is a

Error or Exception o A programs that work when everything is expected is a good start. o When unexpected conditions encountered, it gets really challenging. o Two types of problem: Programmer mistakes and genuine problems. o Errors are dealt by finding and fixing them, by using some codes or numbers.

Error Handling Error handling refers to the response and recovery procedures from error conditions

Error Handling Error handling refers to the response and recovery procedures from error conditions present in a software application. o Anticipation o Detection o Resolution

C - Error Handling o C programming does not provide direct support for error

C - Error Handling o C programming does not provide direct support for error handling o Then how? C is a system programming language

C - Error Handling o A function returns -1 or NULL value in case

C - Error Handling o A function returns -1 or NULL value in case of any error o Global variable errno is set with the error code o Methods and variables regarding error handling defined in error. h header file o The return value can be used to check error while programming.

What is errno? o Whenever a function call is made, a variable named errno

What is errno? o Whenever a function call is made, a variable named errno is associated with it o errno is global variable o Used to identify which type of error was encountered while function execution o Each value errno can take, has some meaning

List of Error numbers errno value Error 1 Operation not permitted 2 No such

List of Error numbers errno value Error 1 Operation not permitted 2 No such file or directory 3 No such process 4 Interrupted system call 5 I/O error 6 No such device or address 7 Argument list too long 8 Exec format error 9 Bad file number 10 No child processes 11 Try again 12 Out of memory 13 Permission denied

Error handling functions o C uses the following functions to represent error messages associated

Error handling functions o C uses the following functions to represent error messages associated with errno o perror(): This function returns a string to pass to it along with the textual representation of current errno value. o strerror(): This function is defined in string. h library and this method return a pointer to the string representation of the present errno value.

Error handling Example-1 #include <stdio. h> #include <errno. h> #include <string. h> int main

Error handling Example-1 #include <stdio. h> #include <errno. h> #include <string. h> int main () { FILE *fp; // If a file, which does not exists, is opened, we will get an error fp = fopen("IWill. Return. Error. txt", "r"); printf("Value of errno: %d n ", errno); printf("The error message is : %s n", strerror(errno)); perror("Message from perror"); return 0; }

Program Exit Status o A common practice to exit with exit status. o EXIT_SUCCESS

Program Exit Status o A common practice to exit with exit status. o EXIT_SUCCESS in case of program coming out after a successful operation o EXIT_FAILURE in case of an error condition. o exit() function is used to inform the calling function about the error. o EXIT_SUCCESS and EXIT_FAILURE macros are defined in stdlib. h header file.

Error handling Example-2 #include <stdio. h> #include <stdlib. h> void main() { int ddend

Error handling Example-2 #include <stdio. h> #include <stdlib. h> void main() { int ddend = 60, dsor = 0, q; if( dsor == 0){ printf("Divide by zero error n"); exit(EXIT_FAILURE ); } q = ddend / dsor; printf("Value of quotient = %d", q); exit(EXIT_SUCCESS); }

Further study on Error Handling: https: //www. studytonight. com/c/error-handling-in-c. php https: //www. tutorialspoint. com/cprogramming/c_error_handling.

Further study on Error Handling: https: //www. studytonight. com/c/error-handling-in-c. php https: //www. tutorialspoint. com/cprogramming/c_error_handling. htm