Home | | Object Oriented Programming and Data Structures | Multiple catch statement with help of suitable C++ coding

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

Multiple catch statement with help of suitable C++ coding

Explain multiple catch statement with help of suitable C++ coding.


Multiple catch statement with help of suitable C++ coding


EXCEPTION HANDLING

 

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

 

 

Multiple catch statements:

 

It is possible that a program segment has more than one condition to throw an exception. In such cases, we can associate more than one catch statement with a try as shown below:

try

{ // try block

}

catch(type1 arg)

{

// catch block1

} catch(type2 arg) {

// catch block2

}

 

 

catch(typeN arg) {

// catch blockN

}

 

Catch All Exceptions:

 

In some situations we may not be able to anticipate all possible types of exceptions we can force a catch statement to catch all exceptions instead of a certain type alone. This is achieved as

 

follows: catch (…)

{

// statement of processing all exceptions

}

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Inheritance and Polymorphism : Multiple catch statement with help of suitable C++ coding |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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