ALGORITHMS AND COMPUTING ( LAB )END SEMESTER PRESENTATIONSGROUP C
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
GROUP MEMBERS:    • Raza Najam    • Hassaan Idrees    • Waleed Raza    • Syed Ahmed Fuad    • Tanveer Hussain    • Hassan ...
PRESENTATION CONTENTS           • Team/Topic Introduction           • Errors & its Types Hassaan   • Exception Handling Ba...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
ERRORS AND ITS TYPES• There are three basic types of errors:         1- Syntax Error         2- Semantic Errors         3-...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
WHAT IS REALLY AN EXCEPTION?• An exception is an indication of a problem that occurs during a program’s execution.• Except...
DIFFERENCES BETWEEN AN ERROR AND AN EXCEPTION1- Errors occur at compilation time as well as run time while exceptionsmostl...
BENEFITS:• Helps improve a programs fault tolerance.• Enables the programmer to remove error-handling code from the ‘mainl...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
EXCEPTION HANDLING IN C• C does not provide direct support for error/exception handling.• By convention, the programmer is...
Exception handling is designed to:   Process synchronous errors, which occur when a statement executes.   Common examples ...
HOW TO HANDLE AN EXCEPTION:-   Ignore the exception                           (Non – Professional )-   Abort the program  ...
THE CLASSIC ‘C’ APPROACH TO EXCEPTION HANDLING.• Each function returns a value indicating success or failure.• Every funct...
Main body{Any program…                   Program Pauses and control isException Occurs   shifted to some other functionFur...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
METHODS OF EXCEPTION HANDLINGThere are two methods to handle an exception in C.1- if - else method2- setjmp() and longjmp(...
METHODS OF EXCEPTION HANDLING1- if - else method     • Exception is handled by making decisions via if – else.     • Occur...
METHODS OF EXCEPTION HANDLING1- if - else method    main()    {    code…    code…    EXCEPTION    if (n==exception conditi...
METHODS OF EXCEPTION HANDLING2- setjmp () and longjmp() method:    • setjmp() and longjmp() are used to jump away from a  ...
METHODS OF EXCEPTION HANDLING2- setjmp () and longjmp() method:    • As soon as the exception test is complete the setjmp(...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
PROGRAM DIFFICULTY LEVEL 1PROGRAM 1:      #include <stdio.h>       void main (void)      {               int a , b , c ;  ...
#include <stdio.h> /* for fprintf and stderr */#include <stdlib.h> /* for exit */                                         ...
#include <setjmp.h>#include <stdio.h>                                                           PROGRAM 3#include <stdlib....
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
CAUSES OF EXCEPTION HANDLINGException Class               CauseArgumentException             An argument to a method was i...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
#include <stdio.h>      /* fprintf */#include <errno.h>       /* errno */                                     PROGRAM 1#in...
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
PROGRAM 1      DL 3
PRESENTATION CONTENTS     • Team/Topic Introduction     • Errors & its Types     • Exception Handling Basics     • Occurre...
GROUP C GRADINGGroup leader: Raza NajamMembers Name    Searching Teamwork PPT Skills Willing to C Presentation Skills Tota...
Q/A
Upcoming SlideShare
Loading in …5
×

Exception handling in c programming

2,803 views

Published on

Learn a few cases where exception occurs in c-language.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,803
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
45
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Exception handling in c programming

  1. 1. ALGORITHMS AND COMPUTING ( LAB )END SEMESTER PRESENTATIONSGROUP C
  2. 2. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  3. 3. GROUP MEMBERS: • Raza Najam • Hassaan Idrees • Waleed Raza • Syed Ahmed Fuad • Tanveer Hussain • Hassan Qamar Rana
  4. 4. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types Hassaan • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) Raza • Methods to handle an Exception Fuad • Example Program 1 ( DL 1) Tanveer • Causes of Exception Occurrence Waleed • Example Program 2 ( DL 2) Raza • Example Program 3 ( DL 3) • Grading • Q/A Session
  5. 5. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  6. 6. ERRORS AND ITS TYPES• There are three basic types of errors: 1- Syntax Error 2- Semantic Errors 3- Logical Errors These errors are detected by the ‘C’ compiler• There are also a few kinds of errors in ‘C’ those are not detected by thecompiler. 4- Run Time Errors 5- Compile Time Errors These error are not identified by the compiler and so they are termedas “EXCEPTIONS”
  7. 7. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  8. 8. WHAT IS REALLY AN EXCEPTION?• An exception is an indication of a problem that occurs during a program’s execution.• Exception is a runtime problem that occurs rarely handling an exception allows programs to continue executing as if no problem had been encountered.• Helps terminating the program in a controlled manner rather in an unpredictable fashion• Exception handling enables programmers to create applications that can resolve (or handle) exceptions.
  9. 9. DIFFERENCES BETWEEN AN ERROR AND AN EXCEPTION1- Errors occur at compilation time as well as run time while exceptionsmostly occur at run time.2- Compile time errors are detected by the compiler while exceptions needto be predicted by the programmer himself3- Errors occur frequently while exceptions, as the name suggests occurseldom4- Error detection and debugging is easier while exception prediction andits handling is a bit complex procedure5- Errors can be removed by removing syntax and logical mistakes whileexception handling needs pre-defined or user defined procedures.
  10. 10. BENEFITS:• Helps improve a programs fault tolerance.• Enables the programmer to remove error-handling code from the ‘mainline’ of the program’s execution• Programmers can decide to handle any exceptions they choose – allexceptions of a certain type or all exceptions of a group of related types
  11. 11. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  12. 12. EXCEPTION HANDLING IN C• C does not provide direct support for error/exception handling.• By convention, the programmer is expected to develop an algorithm foran error and exception case free program.• Intelligent visualization skills and prediction of end-user actions on aparticular event prevents errors from occurring in the first place.• The programmer is expected to test return values from a function.
  13. 13. Exception handling is designed to: Process synchronous errors, which occur when a statement executes. Common examples of these errors are: 1. out-of-range array subscripts 2. arithmetic overflow 3. division by zero 4. invalid function parameters 5. unsuccessful memory allocation, due to lack of memoryException handling is not designed to: Process errors associated with asynchronous events fro example: 1. Disk I/O completions 2. Network message arrivals 3. Mouse-clicks and keystrokes which occur in parallel with, and independent of, the program’s flow control.
  14. 14. HOW TO HANDLE AN EXCEPTION:- Ignore the exception (Non – Professional )- Abort the program ( Even Worst )- Set error indicators ( Beginner’s Approach )- Issue an error-message and call exit(). ( Non – Feasible )- Use setjmp() and longjmp() ( Leading to Professional)
  15. 15. THE CLASSIC ‘C’ APPROACH TO EXCEPTION HANDLING.• Each function returns a value indicating success or failure.• Every function must check the return code of every function call it makes and take care of errors.• Exceptions make it easy to separate error handling from the rest of the code.• Exceptions make it easy to separate error handling from the rest of the code.• Intermediate functions can completely ignore errors occurring in functions they call, if they cant handle them anyway
  16. 16. Main body{Any program… Program Pauses and control isException Occurs shifted to some other functionFurther programEnd of program Now the user defined function decides what to do with that particular exception}
  17. 17. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Example Program 1 ( DL 1) • Causes of Exception Occurrence • Example Program 2 ( DL 2) • Example Program 3 ( DL 3) • Grading • Q/A Session
  18. 18. METHODS OF EXCEPTION HANDLINGThere are two methods to handle an exception in C.1- if - else method2- setjmp() and longjmp() method
  19. 19. METHODS OF EXCEPTION HANDLING1- if - else method • Exception is handled by making decisions via if – else. • Occurrence of an exception is checked by the return values of a function or by defined parameters. • Along with that condition is placed an if – else statement that checks and performs the respective action against that exception.
  20. 20. METHODS OF EXCEPTION HANDLING1- if - else method main() { code… code… EXCEPTION if (n==exception condition) printf(“Operation cannot be performed”); /*here we have to decide whether to exit the program or to ignore the exception and run the program anyway */ else continue code… }
  21. 21. METHODS OF EXCEPTION HANDLING2- setjmp () and longjmp() method: • setjmp() and longjmp() are used to jump away from a particular location in a program into another function. • The programmer written code, inside that function handles the exception • To test the occurrence of an exception the setjmp() function is called up. • setjmp() saves the most recent event of the program before that statement in a buffer called jmp_buf.
  22. 22. METHODS OF EXCEPTION HANDLING2- setjmp () and longjmp() method: • As soon as the exception test is complete the setjmp() returns a value to the function. • If the value returned from the setjmp() to the longjmp() is ‘0’ it means that there was an exception condition. • Now the program will again start from the same point where it stopped (saved in the buffer), until the exception condition is removed or repaired. • On the other hand programmer can also display error messages or other fool-proof techniques.
  23. 23. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  24. 24. PROGRAM DIFFICULTY LEVEL 1PROGRAM 1: #include <stdio.h> void main (void) { int a , b , c ; a=1; b=0; c=a/b printf(“%d”,c); }
  25. 25. #include <stdio.h> /* for fprintf and stderr */#include <stdlib.h> /* for exit */ PROGRAM 2int main( void ) DL 1{ float dividend = 50; float divisor ; float quotient; printf("Enter a divisor for a dividend of 50nn"); scanf("%f", &divisor); if (divisor == 0) { /* Example handling of this error. Writing a message to stderr, and * exiting with failure. */ fprintf(stderr, "nnDivision by zero !!! Aborting... ={ nn"); exit(EXIT_FAILURE); /* indicate failure.*/ } quotient = (dividend/divisor); printf("n%.2fnn",quotient); exit(EXIT_SUCCESS); /* indicate success.*/}
  26. 26. #include <setjmp.h>#include <stdio.h> PROGRAM 3#include <stdlib.h>void main(void) DL 1{ float dividend = 50; float divisor ; float quotient; jmp_buf env; printf("Enter a divisor for a dividend of 50nn"); setjump(env); Stores the scanf() statement in a buffer scanf("%f", &divisor); if (divisor == 0) Exceptional Case longjmp(env,2); Throws back to where we paused quotient = (dividend/divisor); printf("n%.2fnn",quotient); exit(EXIT_SUCCESS); /* indicate success.*/ return 0;}
  27. 27. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  28. 28. CAUSES OF EXCEPTION HANDLINGException Class CauseArgumentException An argument to a method was invalid. A null argument was passed to a method thatArgumentNullException doesnt accept it.ArgumentOutOfRangeException Argument value is out of range.ArithmeticException Arithmetic over - or underflow has occurred. Attempt to store the wrong type of object in anArrayTypeMismatchException array.DivideByZeroException An attempt was made to divide by zero.FormatException The format of an argument is wrong.NotFiniteNumberException A number is not valid.NullReferenceException Attempt to use an unassigned reference.OutOfMemoryException Not enough memory to continue execution.
  29. 29. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  30. 30. #include <stdio.h> /* fprintf */#include <errno.h> /* errno */ PROGRAM 1#include <stdlib.h>#include <string.h> /* malloc, free, exit */ /* strerror */ DL 2extern int errno;int main( void ){ /* pointer to char, requesting dynamic allocation of 2,000,000,000 * storage elements (declared as an integer constant of type * unsigned long int). (If your system has less than 2GB of memory * available, then this call to malloc will fail) */ char *ptr = malloc( 2000000000 ); if ( ptr == NULL ) puts("malloc failed"); else { /* the rest of the code hereafter can assume that 2,000,000,000 * chars were successfully allocated... */ free( ptr ); } exit(EXIT_SUCCESS); /* exiting program */}
  31. 31. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  32. 32. PROGRAM 1 DL 3
  33. 33. PRESENTATION CONTENTS • Team/Topic Introduction • Errors & its Types • Exception Handling Basics • Occurrence & Handling of an Exception ( Flow diagrams ) • Methods to handle an Exception • Programs Difficulty Level 1 • Causes of Exception Occurrence • Programs Difficulty Level 2 • Programs Difficulty Level 3 • Grading • Q/A Session
  34. 34. GROUP C GRADINGGroup leader: Raza NajamMembers Name Searching Teamwork PPT Skills Willing to C Presentation Skills Total/10 10 10 10 10 10 5Hassaan Idrees 8 8 7 7 8 3.8Syed Ahmed Fuad 8 8 5 9 7 3.7Tanveer Hussain 8 8 5 6 5 3.2Waleed Raza 7 7 5 5 4 2.8Hasan Qamar - - - - - -
  35. 35. Q/A

×