Practice on Exceptions

Pages: 12
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.
Last edited on
What do you mean by "invalid inputs"?

hannes
hmmm i think its just inputs that would not be true. I could give you the directory of the lab from where i found it if you would like.

http://horstmann.com/bigcpp/labs/BigC_ch19.htm
Last edited on
You need to read up on exceptions: http://www.cplusplus.com/doc/tutorial/exceptions/

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.
Why don't you just try?
I did try it but im not getting anywhere. I to use this example as a learning device so that i can learn exceptions much better
Post whatever code you have so we can see what's going wrong.
Care to post your try?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
    }
 
}



that is all i have so far :(
Last edited on
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.

Ok i have edited the code at the top but im not sure if its right and can i use any integers as the parameters?
Okay. That looks right. You need to put something in the catch box:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
Last edited on
like this:
 
InvalidArgumentException(x);

No, like this:

class InvalidArgumentException { }; // empty class

And remember to change main() to int main() or int main(int argc, char** argv).

EDIT: and notice you can just put the entire main() function in a try block, like this:

1
2
3
4
5
6
7
8
9
10
int main()
try
{
    computeAge(1990, 1950)
    // some more code
}
catch(InvalidArgumentException& e)
{
    // deal with it
}
Last edited on
so ran this program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace 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
Last edited on
exercise2.cpp:8: error: ‘current’ was not declared in this scope

The following code complies but it does not print anything out when i run it :(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace 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;
    }
 
}
Last edited on
What's this?

exit(0);

Do you mean return 0;? If so, I'd return some other value like 1 or -1 to indicate that the program failed.
Pages: 12