Exceptions – Indicate problems that occur during a program’s execution – Occur infrequentlyException 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
• 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.
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.
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.
Enclose code that may have an error in try block.Follow with one or more catch blocks. Each catch block has an exception handlerIf exception occurs and matches parameter in catch block, code in catchblock executedIf no exception thrown, exception handlers skipped and control resumesafter catch blocksthrow point - place where exception occurred Control cannot return to throw point
• 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
•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
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
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.
•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.
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
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.