Exception Handling in C++ Language
Create a class named Television that has data members to hold the model number and the screen size in inches,and the price for Exception Handling in C++ program.Member functions include overloaded insertion and extraction operators.If more than four digits are entered for the model,if the screen size is smaller than 12 or greater than 70 inches, or if the price is negative or over $5000 then throw an integer.
Write a main() function that instantiates a television object,allows user to enter data and displays the data members .If an exception is caught ,replace all the data member values with zero values .
Exception Handling in C++ Code
#include #include void test(int,int,int); class television { public: int mod_no,price,size; void operator<<(telivision a) { cout<<"\n\t## INPUT ##"; cout<<"\n\tENTER THE MODEL NO.:"; cin>>mod_no; cout<<"\n\tENTER THE SIZE:"; cin>>size; cout<<"\n\tENTER THE PRICE:"; cin>>price; test(mod_no,price,size); getch(); } void operator>>(television a) { cout<<"\n\t## DISPLAY ##)"; cout<<"\nMODEL NO.:"<<mod_no<<"\nsize:"<<size<<"\nprice:"<<price; <br=""> getch(); } }; void test(int mod_no,int price,int size) { if(mod_no>=10000) throw(1); if(price>5000) throw(2); if(price<0) throw(3); if(size<12) throw(4); if(size>70) throw(5); } void main() { int ch,i; television n,o; do { cout<<"\n\t##### MAIN MENU #####"; cout<<"\n\t1.INPUT"; cout<<"\n\t2.DISPLAY"; cout<<"\n\t3.EXIT"; cout<<"\n\tENTER UR CHOICE:"; cin>>ch; switch(ch) { case 1: try { n<<o; <br=""> } catch(int i) { if(i==1) cout<<"\nMODEL NO IS NOT CORRECT"; else if(i==2) cout<<"\nPRICE IS NOT CORRECT"; else if(i==3) cout<<"\nPRICE IS NEGATIVE"; else if(i==4) cout<<"\nSIZE IS VERY SMALL"; else if(i==5) cout<<"\nSIZE IS VERY LARGE"; n.mod_no=0; n.price=0; n.size=0; } getch(); break; case 2: n>>o; getch(); break; case 3: break; } }while(ch!=3); }
Leave a Reply