Error Handling
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
22
1
Outline
 Errors and Its Types
 What is an Exception?
 Differences Between an Error and an Exception
 Benefits of Error Handling
 Exception Handling in C
 Methods of Exception Handling
Errors and Its Types
 There are three basic types of errors:
1. Syntax Error
2. Semantic Errors
3. Logical Errors
Errors and Its Types (cont..)
 These errors are detected by the ‘C’ compiler.There
are also a few kinds of errors in ‘C’ those are not
detected by the compiler.
1. Run Time Errors
2. Compile Time Errors
.These error are not identified by the compiler and so
they are termed as “EXCEPTIONS”
What is 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
Differences Between an Error and an
Exception
 Errors occur at compilation time as well as run time while
exceptions mostly occur at run time.
 Compile time errors are detected by the compiler while
exceptions need to be predicted by the programmer himself
 Errors occur frequently while exceptions, as the name
suggests occur seldom
 Error detection and debugging is easier while exception
prediction and its handling is a bit complex procedure
 Errors can be removed by removing syntax and logical
mistakes while exception handling needs pre-defined or user
defined procedures
Benefits of Error Handling
 Helps improve a program's fault tolerance.
 Enables the programmer to remove error-handling
code from the ‘main line’ of the program’s execution
 Programmers can decide to handle any exceptions
they choose – all exceptions of a certain type or all
exceptions of a group of related types
Exception Handling in C
 C does not provide direct support for
error/exception handling.
 By convention, the programmer is expected to
develop an algorithm for an error and exception case
free program.
 Intelligent visualization skills and prediction of end-
user actions on a particular event prevents errors
from occurring in the first place.
 The programmer is expected to test return values
from a function.
The Classic 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 can't handle them
anyway.
Methods of Exception Handling
 There are two methods to handle an exception in C.
 if - else method
 setjmp() and longjmp() method
1. 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.
1. if - else method (cont..)
main()
{
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…
}
2. 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
2. setjmp () and longjmp() method
(cont..)
 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.
Lecture 22 - Error Handling

Lecture 22 - Error Handling

  • 1.
    Error Handling Md. ImranHossain Showrov (showrovsworld@gmail.com) 22 1
  • 2.
    Outline  Errors andIts Types  What is an Exception?  Differences Between an Error and an Exception  Benefits of Error Handling  Exception Handling in C  Methods of Exception Handling
  • 3.
    Errors and ItsTypes  There are three basic types of errors: 1. Syntax Error 2. Semantic Errors 3. Logical Errors
  • 4.
    Errors and ItsTypes (cont..)  These errors are detected by the ‘C’ compiler.There are also a few kinds of errors in ‘C’ those are not detected by the compiler. 1. Run Time Errors 2. Compile Time Errors .These error are not identified by the compiler and so they are termed as “EXCEPTIONS”
  • 5.
    What is anException?  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
  • 6.
    Differences Between anError and an Exception  Errors occur at compilation time as well as run time while exceptions mostly occur at run time.  Compile time errors are detected by the compiler while exceptions need to be predicted by the programmer himself  Errors occur frequently while exceptions, as the name suggests occur seldom  Error detection and debugging is easier while exception prediction and its handling is a bit complex procedure  Errors can be removed by removing syntax and logical mistakes while exception handling needs pre-defined or user defined procedures
  • 7.
    Benefits of ErrorHandling  Helps improve a program's fault tolerance.  Enables the programmer to remove error-handling code from the ‘main line’ of the program’s execution  Programmers can decide to handle any exceptions they choose – all exceptions of a certain type or all exceptions of a group of related types
  • 8.
    Exception Handling inC  C does not provide direct support for error/exception handling.  By convention, the programmer is expected to develop an algorithm for an error and exception case free program.  Intelligent visualization skills and prediction of end- user actions on a particular event prevents errors from occurring in the first place.  The programmer is expected to test return values from a function.
  • 9.
    The Classic approachTo 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 can't handle them anyway.
  • 10.
    Methods of ExceptionHandling  There are two methods to handle an exception in C.  if - else method  setjmp() and longjmp() method
  • 11.
    1. 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.
  • 12.
    1. if -else method (cont..) main() { 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… }
  • 13.
    2. 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
  • 14.
    2. setjmp ()and longjmp() method (cont..)  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.