Exception handling


Published on

Object Oriented Programming

Published in: Technology
1 Comment
  • For Siebel CRM Online Training Register at http://www.todaycourses.com
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Exception handling

  1. 1. Abhishek PachisiaB-Tech(IT)-II yr09SETIT144
  2. 2. Exceptions – Indicate problems that occur during a program’s execution – Occur infrequentlyException handling – Can resolve exceptions • Allow a program to continue executing or • Notify the user of the problem and • Terminate the program in a controlled manner – Makes programs robust and fault-tolerant
  3. 3. • An exception is a condition that cannot be resolved within the context of the operation that caused it. (examples?)• To throw an exception is to signal that such a condition has occurred.• To catch an exception is to transfer control to an exception handling routine, altering the normal execution flow of the program.• Because exceptions are outside the normal operation of a program the normal, default action is to write out an error message and terminate the offending process.• Older languages did not provide for exception handling as part of the language definition (Fortran, COBOL, C).• Newer languages have added language constructs to facilitate exception handling (Ada, C++, Java).• Most operating systems provide some sort of signal mechanism that can be used to provide a form of exception handling.
  4. 4. Exceptions can occur at many levels: Hardware/operating system level. • Arithmetic exceptions; divide by 0. • Memory access violations; stack over/underflow. Language level. • Type conversion; illegal values, improper casts. • Bounds violations; illegal array indices. • Bad references; null pointers. Program level. • User defined exceptions.
  5. 5.  Intermixing program and error-handling logic – Pseudo code outline Perform a task If the preceding task did not execute correctly Perform error processing Perform next task If the preceding task did not execute correctly Perform error processing Sometimes it is hard to distinguish an exception from normal processing. Makes the program difficult to read, modify, maintain and debug. Impacts performance.Note:– In most large systems, code to handle errors and exceptions represents“>80%” of the total code of the system.
  6. 6. Enclose code that may have an error in try block.Follow with one or more catch blocks. Each catch block has an exception handlerIf exception occurs and matches parameter in catch block, code in catchblock executedIf no exception thrown, exception handlers skipped and control resumesafter catch blocksthrow point - place where exception occurred Control cannot return to throw point
  7. 7. • Use assert – If assertion false, the program terminates• Ignore exceptions – Use this "technique" on casual, personal programs - not commercial!• Abort the program – Appropriate for nonfatal errors give appearance that program functioned correctly – Inappropriate for mission-critical programs, can cause resource leaks• Set some error indicator – Program may not check indicator at all points there error could occur
  8. 8. •Test for the error condition − Issue an error message and call exit. − Pass error code to environment.• setjump and longjump −In <csetjmp> −Jump out of deeply nested function calls back to an error handler. −Dangerous - unwinds the stack without calling destructors for automatic objects (more later).•Specific errors −Some have dedicated capabilities for handling them −If new fails to allocate memory new handler function executes to deal with problem
  9. 9. throw - indicates an exception has occurred Usually has one operand (sometimes zero) of any type. −If operand an object, called an exception object. −Conditional expression can be thrown Code referenced in a try block can throw an exception. Exception caught by closest exception handler. Control exits current try block and goes to catch handler (if it exists). Exception not required to terminate program −However, terminates block where exception occurred
  10. 10. o Exception handlers are in catch blocks.oCaught if argument type matches throw type. If not caught then terminate called which (by default) calls abortoIf no handler matches thrown object •Searches next enclosing try block •If none found, terminate called •If found, control resumes after last catch block •If several handlers match thrown object, first one found is executedocatch parameter matches thrown object when •They are of the same type. • The catch parameter is a public base class of the thrown object. •The catch parameter is a base-class pointer/ reference type and the thrown object is a derived-class pointer/ reference type.
  11. 11. •The catch handler is catch( ... ). •Thrown const objects have const in the parameter type.oUnreleased resources •Resources may have been allocated when exception thrown catch handler should delete space allocated by new and close any opened files.ocatch handlers can throw exceptions •Exceptions can only be processed by outer try blocks.
  12. 12. oUsed when an exception handler cannot process an exception.oRethrow exception with the statement: throw; •No arguments •If no exception thrown in first place, calls terminateoHandler can always rethrow exception, even if it performed someprocessingoRethrown exception detected by next enclosing try block