Archive
Archive for January 28, 2010
C++ Program to implement Exception Handling by using TRY CATCH – Q28
January 28, 2010
Leave a comment
Q28. Program to implement Exception Handling by using TRY CATCH:
Write a Boolean function that returns TRUE/FALSE if the unsigned int argument passed to it is a Leap Year or Non Leap Year. The function must throw an “Out Of Range” exceptio if its argument does not lie between 0 and 2100.
… from College notes (BCA/MCA assignments):
#include <iostream.h> #include <conio.h> enum boolean{false, true} bool; class Cyear{ private: int dd, mm, yy; public: void getDate(); int isLeap(); void putDate(); }; void Cyear :: getDate(){ cout<<"\n Enter Date (dd mm yyyy): "; cin>>dd>>mm>>yy; try{ if( (yy < 0) || (yy > 2100) ) throw "Out of Range"; } catch(char *errmsg){ cout<<"\n ERROR: "<<errmsg; } } int Cyear :: isLeap (){ return ( (yy % 400 == 0) || ((yy % 4 == 0) && (yy % 100 != 0)) ); } void Cyear :: putDate(){ if(isLeap()) cout<<"\n Is a leap year."; else cout<<"\n Is not leap year."; } void main(){ clrscr(); Cyear Odt; Odt.getDate(); Odt.putDate(); getch(); }
Output:
Enter a Date (dd mm yyyy): 18 11 2004
Is a Leap Year.
Enter a Date (dd mm yyyy): 18 11 2290
ERROR: Out of Range.
Is not a Leap Year.
Categories: Cpp
Cpp, Exception Handling, Leap Year, TRY-CATCH