Loading....
Coupon Accepted Successfully!

 

Guidelines For Exception Handling

The C++ exception-handling mechanism provides three keywords; they are try, throw, and catch. The keyword try is used at the starting of the exception. The throw block is present inside the try block. Immediately after the try block, the catch block is present. Figure 19.1 shows the try, catch, and throw statements.

As soon as an exception is found, the throw statement inside the try block throws an exception (a message for the catch block that an error has occurred in the try block statements). Only errors occurring inside the try block are used to throw exceptions. The catch blockreceives the exception that is sent by the throw block. The general form of the statement is as per Figure 19.2.

When the try block passes an exception using the throw statement, the control of the program passes to the catch block. The data type used by throw and catch statements should be same; otherwise, the program is aborted using the abort() function, which is executed implicitly by the compiler. When no error is found and no exception is thrown, in such a situation, the catch block is disregarded, and the statement after the catch block is executed.

127043.png

Fig. 19.1 Exception mechanism

Fig. 19.2 try, throw, and catch blocks

19.1 Write a program to throw exception when j=1 otherwise perform the subtraction of x and y.

#include<iostream.h>

#include<conio.h>

int main()

{

int x,y;

cout<<“\n Enter values of x and y\n”;

cin>>x>>y;

int j;

j=x>y ? 0 :1;

try

{

if (j==0)

{ cout<<“Subtraction (x-y)=”<<x-y<<“\n”; }

else { throw(j); }

}

catch (int k)

{

cout<<“Exception caught : j =”<<j <<“\n”;

}

return 0;

}

}

OUTPUT

Enter values of x and y

7 8

Exception caught : j = 1

Enter values of x and y

4 8

Exception caught : j = 1

Explanation: In the above program, the values of x and y are entered. The conditional operator tests the two numbers; if x is greater than y, zero is assigned to j; otherwise, it is one. In the try block, the if condition checks the value of j; the subtraction is carried out when j is zero; otherwise, the else block throws the exception. The catch statement catches the exception thrown. The output of the program is shown above.

19.2 Write a program to define function that generates exception.

#include<iostream.h>

void sqr()

{

int s;

cout<<“\n Enter a number:”;

cin>>s;

if (s>0)

{

cout<<“Square=”<<s*s;

}

else

{

throw (s);

}

}

int main()

{

try

{

sqr();

sqr();

}

catch (int j)

{

cout<<“\n Caught the exception \n”;

}

return 0;

}

OUTPUT

Enter a number : 5

Square = 25

Enter a number : 0

Caught the exception

Explanation: In the above program, the function sqr() is defined. The function sqr() reads an integer through the keyboard and displays its square. Before calculating the square, the if statement checks the number. If it is greater than zero, then the if block calculates the square and displays it; otherwise, the else block throws the exception. In the function main(), in the try block, the function is called twice. In case an exception is thrown from the function sqr(), the catch statement catches it, and the catch block is executed. This happens only when the user enters the number 0 or less than zero. If the user enters 0 in the first call, then the exception is thrown, and the catch block is executed. The compiler ignores the next statement of the try block. Once the control skips from the try block, it never comes back to execute the remaining statements. Thus, in this program if in the first call the user enters 0, then the second call of the function sqr() is not taken into account.





Test Your Skills Now!
Take a Quiz now
Reviewer Name