Advertisements
Home > Cpp > C++ Program to implement Exception Handling by using TRY CATCH – Q28

C++ Program to implement Exception Handling by using TRY CATCH – Q28


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.


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: