Can someone start this code off for me so that i can see how its done? This is one of the practice problems but i would like to get as much practice as i can with exceptions.
1 2 3 4
int computeAge(int currentYear, int birthDate)
{
return currentYear – birthDate;
}
-Implement a main function function, in which you have a try statement. In this try statement, call the computeAge() function with invalid inputs. A catch statement should catch the exception, print out an error message, and cause the program to immediately exit.
-Define and implement the InvalidArgumentException class such that it stores a single text error message initialized by the class’s constructor.
I would imagine that validating input requires checking that the birthDate is less than or equal to the currentYear and that both numbers are positive.
I read about exceptions and now im trying to understand how to do this code. Thats why i wanted someone to do it so i can study it and get a better grasp of exceptions.
OH and i forgot this piece of the question: Supply code for the computeAge() function below such that an exception of type InvalidArgumentException is thrown whenever either of the two inputs is negative or if the current year is less than the date of birth.
int computeAge(int currentYear, int birthDate)
{
if (currentYear < 0 || birthDate < 0 || current year < birthDate)
throw InvalidArgumentException();
}
int main(int currentYear, int birthDate)
{
try
{
computeAge(1990, 1950)
}
catch ( )
{
cout<<"Attempted to enter negate currentYear, birthdate or current year less than birthdate ";
exit(0);
}
}
Okay, that's good. You now need to move your if (currentYear < 0 || birthDate < 0 || current year < birthDate) code into your function computeAge(). It is in that function that you need to test the parameters and decide to throw the exception or not.
Keep the try{}catch(){} in your main() function and call your computeAge() function from inside the try{} block.
There's quite a few problems there. First, main() doesn't take whatever types of argument you want. Second, there's no computeAge() function. Third, you forgot to throw the exception. Fourth, you didn't define InvalidArgumentException as a type, so you can't expect the compiler to know what you're talking about.
int computeAge(int currentYear, int birthDate)
{
if (currentYear < 0 || birthDate < 0 || current year < birthDate)
throw InvalidArgumentException();
}
int main(int currentYear, int birthDate)
{
try
{
computeAge(1990, 1950)
}
catch (InvalidArgumentException& e) // put what you want to catch here.
{
cout<<"Attempted to enter negate currentYear, birthdate or current year less than birthdate ";
exit(0);
}
}
Also you need to define the type InvalidArgumentException so that the compiler knows what it is.
#include <iostream>
usingnamespace std;
int computeAge();
class InvalidArgumentException { };
int computeAge(int currentYear, int birthDate)
{
if (currentYear < 0 || birthDate < 0 || currentYear < birthDate)
throw InvalidArgumentException();
}
int main(int argc, char** argv)
{
try
{
computeAge(1990, 1950)
}
catch (InvalidArgumentException& e)
{
cout<<"Attempted to enter negate currentYear, birthdate or current year less than birthdate ";
exit(0);
}
}
and i got this error:
1 2 3 4 5
g++ exercise2.cpp -o exercise2
exercise2.cpp: In function ‘int main(int, char**)’:
exercise2.cpp:18: error: expected ‘;’ before ‘}’ token
exercise2.cpp:22: error: ‘exit’ was not declared in this scope
make: *** [exercise2] Error 1
#include <iostream>
usingnamespace std;
int computeAge();
class InvalidArgumentException { };
int computeAge(int currentYear, int birthDate)
{
if (currentYear < 0 || birthDate < 0 || currentYear < birthDate)
throw InvalidArgumentException();
}
int main(int argc, char** argv)
{
try
{
computeAge(1990, 1950);
}
catch (InvalidArgumentException& e)
{
cout<<"Attempted to enter negate currentYear, birthdate or current year less than birthdate ";
return 0;
}
}