To Show exception handling.
//Try block throwing exception
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter value of A and B"<<endl;
cin>>a>>b;
try
{
if(b==0)
throw b;
else
cout<<"Result="<<(float)a/b<<endl;
}
catch(int i)
{
cout<<"Divide by zero exception: B="<<i<<endl;
}
cout<<"END"<<endl;
getch();
return 0;
}
#include <conio.h>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter value of A and B"<<endl;
cin>>a>>b;
try
{
if(b==0)
throw b;
else
cout<<"Result="<<(float)a/b<<endl;
}
catch(int i)
{
cout<<"Divide by zero exception: B="<<i<<endl;
}
cout<<"END"<<endl;
getch();
return 0;
}
Output:-
First Execution
Second Execution
0 comments:
Post a Comment