Jumat, 08 Juli 2011

Perform exception handling for Divide by zero Exception

perform exception handling for Divide by zero Exception.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03void main()
04{
05   int a,b,c;
06   float  d;
07   clrscr();
08   cout<<"Enter the value of a:";
09   cin>>a;
10   cout<<"Enter the value of b:";
11   cin>>b;
12   cout<<"Enter the value of c:";
13   cin>>c;
14 
15   try
16   {
17              if((a-b)!=0)
18              {
19                 d=c/(a-b);
20                 cout<<"Result is:"<<d;
21              }
22              else
23              {
24                 throw(a-b);
25              }
26   }
27 
28   catch(int i)
29   {
30              cout<<"Answer is infinite because a-b is:"<<i;
31   }
32 
33   getch();
34}
Output:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40

Tidak ada komentar:

Posting Komentar