Best C++ Build in - Userdefine Exception
Best C++ Build in - Userdefine Exception
#include <iostream>
#include <stdexcept>
using namespace std;
int main()
{
int num;
try
{
cout<<"Enter no ";
cin>>num;
if(cin.fail())
throw "Input must be integer";
cout<<"No is "<<num<<endl;
}
catch(const char *msg)
{
cout<<msg<<endl;
}
cout<<"Hello class"<<endl;
cout<<"C++ exception"<<endl;
return 0;
}
……………………………………………….
/// Input Mismatch
#include <iostream>
#include <stdexcept>
using namespace std;
int main()
{
int num;
try
{
cout<<"Enter no ";
cin>>num;
if(cin.fail())
throw runtime_error("input must be integer");
cout<<"No is "<<num<<endl;
}
catch(runtime_error& e)
{
cout<<"Error..."<<e.what()<<endl;
}
cout<<"Hello class"<<endl;
cout<<"C++ exception"<<endl;
return 0;
}
………………………………………………..
//Input Mismatch + denominator must be > Zero
/// C++ Exceptions
#include <iostream>
#include<stdexcept>
using namespace std;
int main()
{
int no1, no2;
try
{
cout<<"Enter No-1 ";
cin>>no1;
if(cin.fail())
throw runtime_error("No-1 must be integer");
cout<<"Enter No-2 ";
cin>>no2;
if(cin.fail())
throw runtime_error("No-2 must be integer");
if(no2 == 0)
throw runtime_error("/ by zero");
cout<<"Division result is "<<(float) no1/no2<<endl;
}
catch(runtime_error& e)
{
cout<<"Error....."<<e.what()<<endl;
}
cout<<"Hello Class"<<endl;
cout<<"C++ Exceptions"<<endl;
return 0;
}
……………………………………………………….
/// stack Class with build in Exception
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
using namespace std;
class Stack
{
private:
int arr[5];
int top;
public:
Stack():top(-1){}
void Push(int var)
{
if(top >= 4)
{
cout<<"Stack overflow"<<endl;
exit(1);
}
arr[++top] = var;
}
int pop()
{
if(top == -1)
{
cout<<"Stack under flow"<<endl;
exit(1);
}
return arr[top--];
}
};
int main()
{
Stack s;
s.Push(11);
s.Push(12);
s.Push(13);
s.Push(14);
s.Push(15);
//s.Push(16);
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
///cout<<"value is "<<s.pop()<<endl;
cout<<"Bye Bye"<<endl;
return 0;
}
…………………………………………………………………
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
using namespace std;
class Stack
{
private:
int arr[5];
int top;
public:
Stack():top(-1){}
void Push(int var)
{
if(top >= 4)
throw runtime_error("Stack overflow");
arr[++top] = var;
}
int pop()
{
if(top == -1)
throw runtime_error("Stack under flow");
return arr[top--];
}
};
int main()
{
Stack s;
try
{
s.Push(11);
s.Push(12);
s.Push(13);
s.Push(14);
s.Push(15);
///s.Push(16);
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
}
catch(runtime_error& e)
{
cout<<"Error..."<<e.what()<<endl;
}
cout<<"Bye Bye"<<endl;
return 0;
}
……………………………………………………….
#include<iostream>
#include<stdexcept>
using namespace std;
class Distance
{
private:
int feets;
float inches;
public:
Distance(): feets(0),inches(0.0f){}
friend istream& operator>>(istream&, Distance&);
friend ostream& operator<<(ostream&,Distance&);
int main()
{
try
{
Distance d1, d2, d3;
cin >> d1;
cin >> d2;
d3 = d1 / d2;
return 0;
}
……………………………………………..
/// stack Class with build in Exception (inherited)
#include <iostream>
#include <stdlib.h>
#include <stdexcept>
using namespace std;
#include<iostream>
#include<stdexcept>
using namespace std;
class DivideByZero : public runtime_error
{
public:
DivideByZero():runtime_error(){}
DivideByZero(char ch[]):runtime_error(ch){}
};
int main()
{
try
{
int num,dnum;
cout<<"Enter num ";
cin>>num;
cout<<"Enter D-num ";
cin>>dnum;
if(dnum == 0)
{
throw DivideByZero (“Error…/ by Zero”);
}
cout<<"Result :"<<num / dnum<<endl;
}
catch(DivideByZero d)
{
cout<<d.what()<<endl;
}
return 0;
}
………………………………………..
//user define exception Stack with separate Exception
#include <iostream>
#include <stdexcept>
using namespace std;
class MyException : public runtime_error
{
public:
MyException(char ch[]):runtime_error(ch){}
};
class Stack
{
private:
int arr[5];
int top;
public:
Stack():top(-1){}
void Push(int var)
{
if(top >= 4)
throw MyException("Stack Over Flow");
arr[++top] = var;
}
int pop()
{
if(top == -1)
throw MyException("Stack Under Flow");
return arr[top--];
}
};
int main()
{
Stack s;
try
{
s.Push(11);
s.Push(11);
s.Push(11);
s.Push(11);
s.Push(11);
//s.Push(11);
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
cout<<"value is "<<s.pop()<<endl;
// cout<<"value is "<<s.pop()<<endl;
}
catch(MyException e)
{
cout<<e.what()<<endl;
}
return 0;
}
……………………………………….
Question: Create a Professor class that has data members to holds the Id (int), name (string) and
Pub (int). Class also includes parameterized constructors and overloaded insertion (<<) and
extraction (<<) operators’ that displays and get all fields of class Professor. Create an
ProException class that holds EstimPub (type int). When the user enters Professor data, if the
pub is below then 10, then throw an ProException object with an appropriate message (Pass this
String to the ProException’s parent so it can be used in a what () call). Write a main () function
that instantiates a Professor object, allows the user to enter data, and displays the data members.
1st method
//user define exception
#include <iostream>
#include <stdexcept>
#include <cstring>
using namespace std;
class Professor
{
private:
int id, pub;
char name[20];
public:
class ProException :public runtime_error
{
public:
int expub;
ProException(int p, string s): runtime_error(s), expub(p){}
};
2nd method
class Professor
{
private:
int id, pub;
char name[20];
public:
int main()
{
try
{
Professor p(1,"",100);
cin>>p;
cout<<p;
}
catch(ProException& e)
{
cout<<endl;
cout<<"Error.."<<" Professor id is "<<e.expub<<endl;
cout<<e.what();
cout<<endl;
}
return 0;
}