12
Object Oriented Programming Abhishek Pachisia B-Tech(IT)-II yr 09SETIT144

Exception handling

Embed Size (px)

DESCRIPTION

Object Oriented Programming"C++"

Citation preview

Page 1: Exception handling

Object Oriented Programming

Abhishek PachisiaB-Tech(IT)-II yr09SETIT144

Page 2: Exception handling

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

Exception Handling

Page 3: Exception handling

• 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.

Introduction

Page 4: Exception handling

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.

Exceptions can occur at many levels:

Exception Levels

Page 5: Exception handling

Intermixing program and error-handling logic– Pseudo code outline

Perform a taskIf the preceding task did not execute correctly Perform error processingPerform next taskIf 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.

Page 6: Exception handling

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 catch block executedIf no exception thrown, exception handlers skipped

and control resumes after catch blocksthrow point - place where exception occurred

Control cannot return to throw point

Format Of Exception Handling

Page 7: Exception handling

Error Handling Techniques• 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

Page 8: Exception handling

•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

Page 9: Exception handling

throw - indicates an exception has occurredUsually has one operand (sometimes zero) of any type.

− If operand an object, called an exception object.

−Conditional expression can be thrownCode 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

Throwing An Exception

Page 10: Exception handling

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.

Page 11: Exception handling

• The catch handler is catch( ... ).• Thrown const objects have const in the parameter

type.oUnreleased resources• Resources may have been allocated when exception

throwncatch 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.

Page 12: Exception handling

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 terminate

oHandler can always rethrow exception, even if it performed some processing

oRethrown exception detected by next enclosing try block

That’s All