Professional Documents
Culture Documents
Department of Computer Science: Bahria University, Lahore Campus
Department of Computer Science: Bahria University, Lahore Campus
Department of Computer Science: Bahria University, Lahore Campus
Objective(s):
Upon completion of this lab session, learners will be able to:
Exception Handling in C++
Lab Tasks:
Task 1
Write a program that prompts the user to enter a length in feet and inches and outputs the
equivalent length in centimeters. If the user enters a negative number, throw and handle an
appropriate exception and prompt the user to enter another set of numbers.
#include<iostream>
#include<string>
using namespace std;
int main()
try
if (feet<0)
throw(feet);
if (inch<0)
throw(inch);
%
Enrollment Number: ____________________________
cent = total*2.54;
cout << feet << " feet and " << inch << " inch is "
{
cout << "You entered an invalid number for inch, please try
again." << endl;
}}
return 0;
Task 2
Write a program that prompts the user to enter a person’s date of birth in numeric form such
as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your
program must contain at least two exception classes: invalidDay and invalidMonth. If the
user enters an invalid value for day, then the program should throw and catch an
invalidDayobject. Similar conventions for the invalid values of month and year.
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 2 of 2