Home | | Programming and Data Structure II | Exception Handling

Chapter: Programming and Data structures : C++ Programming Advanced Features

Exception Handling

An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing.

EXCEPTION HANDLING

 

An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing.


WITHOUT EXCEPTION HANDLING

 

When an exception occurs, control goes to the operating system, where typically

• an error message is displayed

• the program is terminated

 

WITH EXCEPTION HANDLING

 

• Programs are allowed to trap exceptions.

• There is a possibility to fix the problem and continuing execution

 

Exception handling mechanism

 

To detect and report error, The error handling code performs the following task

 

1.  Find the problem (Hit the exception)

 

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

 

3. Receive the error information. (Catch the exception)

 

4. Take corrective actions. (Handle the exception).

 

Keywords in Exception Handling

 

·        try

 

·        throw

 

·        catch

 

 

TRY BLOCK -The keyword try is used to preface a block of statements (surrounded by braces) which may generate exceptions.

 

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

 

CATCH BLOCK - defined by the keyword catch „catches‟ the exception „thrown‟ by the throw statement in the try block, and handles it appropriately.


Exception Handling Syntax:

 

try                                                   //try block

 

{

 

…….

 

throw exception;  // Block of statements which detects and throws an exception

 

}

 

catch(type arg) // Catches exception

 

{

 

…….

 

…….. // Block of statements that handles the exception

 

}

 

 

Example:   

#include <iostream>     

int main()   

cout << “start”;   

try    

{        // start

a  try  block  cout  <<  Inside  try

block\n”;    

throw 100;  // throw an error

cout << “This will not execute”;

}       

catch (int i)

{        // catch an error

 

cout << “caught an exception “; cout << i ; cout << “End”; return 0;

 

}

 

}

 

 

Exception Types

 

Synchronous Exception:

 

•        Out of range

 

•        Over flow

 

Asynchronous Exception

 

•        Error that are caused beyond the control of the program

 

•        Keyboard interrupts

In C++ only synchronous exception can be handled.

 

 

Factors determining Exception

 

·        Division by zero

 

·        Access to an array outside of its bounds

 

·        Running out of memory

 

·        Running out of disk space

 

 

Need for Exception Handling

 

·  Dividing the error handling

 

·  Unconditional termination & programmer preferred termination

 

·    Separating error reporting and error handling

 

·  Object destroy problem

 


 

·        When the program enters the try block it is said to be in the guarded section.

 

·        In this program when the value of j is zero an exception is created and thrown.

 

·        Note that the statement after throw statement in try block is not executed.

 

·        Once the exception is thrown the catch block catches the value (here zero) and handles it.

 

·        After that the program continues its normal execution.

 

 

Functions Generating Exceptions

 

C++ allows functions to generate exceptions. These functions cannot be called as an ordinary function. Enclose the function call with a try catch block.

 

Syntax:

 

try

 

{ function(arg);

 

}

 

catch(type arg)

 

{

 

------

 

}

 


 

Throwing Mechanisms:

 

·        An exception is thrown by using the throw keyword from inside the try block.

 

·        A throw expression accepts one parameter, which is passed as an argument to the

 

exception handler.

 

Eg. throw b;

 

The throw statement will have the following.

 

Example:

 

·        throw (exception)

 

·        throw exception

 

·    throw

 

 

Catching Mechanisms

 

·        If the data type specified by a catch, matches that of the exception, then catch statement is executed.

 

·        When an exception is caught, arg will receive its value

 

 

Multiple Catch Statements

 

·        A try can have multiple catches

 

·        If there are multiple catches for a try, only one of the matching catch is selected and that corresponding catch block is executed.

 

 

Syntax:

 

try

 

{

 

any statements

 

if (some condition) throw value1;

else if (some other condition) throw value2;

 

else if (some last condition) throw valueN;

 

}

 

catch (type1 name)

 

 

{

 

any statements

 

}

 

catch (type2 name)

 

{

 

any statements

 

}

 

catch (typeN name)

 

{

 

any statements

 

}

 

 

Example:

 

#include<iostream.h>

 

void multiple_catch(int value)

 

{

 

try

 

{

 

if (value==0) //throw an int value throw 1;

 

else if (value==1) //throw a char throw „a‟;

 

else //throw float throw 1.1;

 

}

 

catch(char c)

 

{

 

cout<<”Character value is thrown” <<c;

 

}

 

catch(int i)

 

{

 

cout<<”Integer value is thrown”<<i;

 

}

 

catch(float f)

 

{

 

cout<<”Float value is thrown”<<f;

 

}

 

}

 

void main()

 

{

 

cout<<”Main Program”<<endl;

multiple_catch(0);

 multiple_catch(1);

 multiple_catch(5);

}

 

 

Rethrowing Exceptions

 

Syntax:

 

try

 

{

 

…. throw a;

}

 

catch (char c)

 

{

 

throw;        //rethrow same exception in catch block in main()

 

}

 

 

Example:

 

#include <iostream.h>

 

class sam

 

{

 

int erno;

 

public:

 

sam (int errno)

 

{

 

exno=errno;

 

}

 

void shoex()

 

{

 

cout<<”error no:”<<exno;

 

}

 

};

 

void ergen()

 

{

 

try

 

{

 

sam s1(20); int c;

 cin>>c; switch (c)

 

{

 

case 1: throw 10;

 case 2: throw „a‟;

 case 3: throw s1;

 case 4: throw “welcome”;

 

}

 

catch (int ie)

 

{

 

cout<<”ie”<<ie; throw; //rethrowing

}

 

catch (char ce)

 

{

 

cout <<”ce”<<ce; throw; //rethrowing

}

 

}

 

void main ()

 

{

 

try

 

{

 

ergen(); throw 10;

}

 

catch (int)

 

{

 

cout <<”caught integer”;

 

}

 

catch(char)

 

{

 

cout<<”caught char”;

 

}

 

catch (sam s2)

 

{

 

s2.show x();

 

}

 

 

Terminate Function

 

Terminate () is the function which calls abort() to exit the program in the event of run time error related to exceptions. The user can provide his or her own terminate function instead of built-in terminate.

 

Use:

 

Used to close all open files & deallocate resources before quitting the program.

 

Syntax: set_terminate (myterminate);

 

 

Unexpected Function

 

·        If a function throws an exception which is not allowed, a function unexpected () is called, which in turn calls abort.

 

·        We can use set_unexpected in a similar to set_terminate Syntax: set_unexcepted(my unexcepted);

 

Example:

 

#include <iostream.h>

void myunexpected ()

{

 

cout << "unexpected called\n";

 

throw 0;   // throws int (in exception-specification)

 

}

 

void myfunction () throw (int)

{

 

throw 'x'; // throws char (not in exception-specification)

 

}

 

int main (void)

 

{

 

set_unexpected (myunexpected);

 

try

 

{

 

myfunction();

 

}

 

catch (int)

 

{

 

cout << "caught int\n";

 

}

 

catch (...)

 

{

 

cout << "caught some other exception type\n";

 

}

 

return 0;

 

}

 

 

Uncaught Exception()

 

·        This function returns true if an exception has been thrown but not yet caught.

 

·        Once caught, the function returns false.

 

Syntax:

 

bool uncaught_exceptions. if (uncaught_exception(

))

 

{

 

//Do not call the function which might throw an exception

 

}

 

Otherwise

 

{

 

Follow the natural sequence of the destructor Algorithm

 

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data structures : C++ Programming Advanced Features : Exception Handling |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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