Home | | Object Oriented Programming and Data Structures | Need for exception with try, catch and throw keywords

Chapter: Object Oriented Programming and Data Structure : Inheritance and Polymorphism

Need for exception with try, catch and throw keywords

Discuss the need for exception with try, catch and throw keywords.

The need for exception with try, catch and throw keywords.

 

Exceptions are run time anomalies or unusual conditions that a program may encounter while executing. Anomalies might include conditions such as division by zero, access to an array outside of its bounds, or running out of memory or disk space. When a program encounters an exceptional condition, it is important that it is identified and dealt with effectively.

 

Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions. Errors such as “out-of-range index” and “over flow” belong to the synchronous

exceptions. The errors that are caused by events beyond the control of the program are called asynchronous exceptions. The proposed exception handling mechanism is designed to handle only synchronous exceptions.

 

The mechanism performs following tasks:

 

·        Find the problem (Hit the exception).

·        Inform that an error has occurred (Throw the exception).

·        Receive the error information (Catch the expression).

·        Take corrective actions (Handle the exceptions).

 

The error handling code basically consists of two segments one to detect errors and to throw exceptions, and other to catch the exceptions and to take appropriate actions.

 

Exception Handling Mechanism:

·        It is built upon three keywords, namely, try, throw and catch.

·        The keyword try is used to preface a block of statements which may generate exceptions.

·        This block of statements is known as try block.

When an exception is detected, it is thrown using a throw statement in the try block.

 

 

 

A catch block is defined by the keyword catch ‘catches’ the exception ‘thrown’ by the throw statement in the try block, and handles it appropriately. If the type of object thrown matches the arg type in the catch statement, then catch block is executed for handling the exception. If they do not match the program is aborted with the help of the abort() function is invoked by default. When no exception is detected and thrown, the control goes to the statement immediately after the catch block. Most often exceptions are thrown by the functions that are invoked from within the try blocks.

 

The point at which the throw is executed is called the throw point.

 

The general format of code for this kind of relationship is shown below:

Example:

#include

void divide(int a, int b)

{

if(b!=0)

 

cout << “Result = “ << a/b;

else

throw(b);

}

 

void main()

{

 

cout << “Enter two numbers “;

int x,y;

 

cin >> x >> y; try

{

divide(x,y);

}

catch(int i)

{

cout << “Error! Dividing by Zero “;

 

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Inheritance and Polymorphism : Need for exception with try, catch and throw keywords |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.