Exception-Handling

Views:
 
Category: Education
     
 

Presentation Description

Hey this PPT is all about the concept of Exception Handling.

Comments

Presentation Transcript

Exception Handling:

Exception Handling

PowerPoint Presentation:

What is Exception We often come across some peculiar problem other than logical and syntax error. They are known as exception. Exception indicate that occur during a program execution.

Exception-Handling:

Exception-Handling Type safe , Integrated approach C++ Exception handling provides :- Coping with the unusual predictable problems that arise while executing a program. With exception handling a program can continue executing (rather than terminating) after dealing a problem.

Exception-Handling can resolve exception.:

Exception-Handling can resolve exception. Allow a program to continue execution. Notify the User problem. Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called Handlers. Terminate the programs in control manner.

When Exception Handling Should Be Used:

Processing exceptional situations. Processing exceptions for components that cannot handle them directly. Large projects that require uniform error processing. When Exception Handling Should Be Used

Basics of Exception-Handling:

Basics of Exception-Handling Synchronous Exception Error such as “out of range index” and “ over-flow” belongs to Synchronous Exception. 2. Asynchronous Exception Errors that are cause by event beyond the control to programme (such as keyboard interupt ) are called A synchronous Exception.

Exception Handling Mechanism:

Exception Handling Mechanism C++ Exception handling mechanism is basically built upon 3 keyword. Try-Block Throw-Block Catch-Block

PowerPoint Presentation:

Try-Block Keyword try is used to preface a block of statement (surrounded by braces). The block of statement is known as Try Block try { ……… ………… } Syntax of try block

PowerPoint Presentation:

Throw-Block Keyword Throw followed by an operand representing the type of exception. Throw operand object exception may be of any type , including Constant If throw operand is an object, it is called an Exception object. Throw operand initializes the exception parameter in the matching , if one is found.

PowerPoint Presentation:

Catch Block Catch block defined by the keyword Catch Represent the type of exception that catch block handles. Exception handling code is placed between two braces {} . Exception parameter enclosed in parentheses

PowerPoint Presentation:

The catch statement catches an exception whose type matches with the type of catch argument. Catch (type arg) { //statement for //managing exceptions } Executes if exception parameter type matches the exception thrown in the try block

PowerPoint Presentation:

The keyword try is used to preface a block of statements(surrounded by braces) which may generated exceptions. When an exception is detected, it is thrown using a throw statement in a try block. A catch block defined by the keyword catch ‘catches’ the exception ‘thrown’ by the throw statement in the try block and handle it appropriately. The catch block that catches an exception must immediately follow the try block that the exception. Try block Detects and throws an exception Catch block Catches and handles the exception Exception object

PowerPoint Presentation:

Try { ….. throw exception; // block of statement which ….. // detects and throw an exception ….. } Catch(try arg) // Catches of exception { ….. // block of statement that ….. // handles the exception } When try block throws an exception, the program control leaves the try block and enter the catch system.

PowerPoint Presentation:

Exception are object use to transmit information about a problem, if the type of object throw match the argument( arg ) type in catch system , then catch block is executed for handling the exception. Note:-

PowerPoint Presentation:

#include< iostream.h > using namespace std; int main() { //code int x=a-b; Try { if ( x! =0 ) { cout<< “Result(a/x)=“<<a/x ; } else { Throw (x); } } catch ( int i ) { Cout<<“Exception caught :x =“<<x” } Cout<<“End”; Return 0; } Output of Program First run Enter value of a and b 20 15 Result (a/x)= 4 End & Second run Enter value of a and b 10 10 Exception caught: x=0 End

PowerPoint Presentation:

You can nest the try-blocks. Exception handling was not part of the original C++. It is new features added to ANSI c++ . C++ provides mechanism for handling Synchronous exception . All throws were caught by the catch(..) statement. After exception handling the program return to the point after the try-catch block, not after the throw statement. Some points to remember

PowerPoint Presentation:

Thank you