Exception Program with Classes

I'm having trouble with understanding how classes are used with exceptions. Since I'm working with classes, do I still have to work with public/private and then use void?

I'm also confused about throw/catch. I believe I have the catch part right, but I don't know if the part throw part for invalidDay is right. I want the program to basically be able to detect that inputting 31 for a day in, let's say, January is invalid.

Assignment: 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
invalidDay object. Similar conventions for the invalid values of month
and year. (Note that your program must handle a leap year.).

I would appreciate it if someone would guide me to the correct direction.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

#include <string>
using namespace std;

int day;
int month;
int year;

 
void inputDate (int) throw (int day, invalidDay, invalidMonth);

int main()
{
    try 
    {

        cout << "\nEnter your birthday here in this format: mm/dd/yyyy--->: ";

        cout <<"Enter day here."; 
        cin >> day; 

        cout <<"Enter the month here."; 
        cin >> month;

        cout <<"Enter the year here.";
        cin >> year; 

        if (day > 31) 
            throw invalidDay;  

        
        if(month==1)
        cout << "January ";
    else
        if(month==2)
        cout << "February ";
    else
        if(month==3)
        cout << "March ";
    else
        if(month==4)
        cout << "April ";
    else
        if(month==5)
        cout << "May ";
    else
        if(month==6)
        cout << "June ";
    else
        if(month==7)
        cout << "July ";
    else
        if(month==8)
        cout << "August ";
    else
        if(month==9)
        cout << "September ";
    else
        if(month==10)
        cout << "October ";
    else
        if(month==11)
        cout << "November ";
    else
        if(month==12)
        cout << "December ";
    else 
        if (month > 12)
    throw invalidMonth;

    }

    catch (invalidDay)
    {
        cout <<"The value that you entered is invalid, try again."; 
    }
    catch (invalidMonth)
    {
        cout <<"The month you entered is invalid, try again."; 
    }

 
cout << "The date is " << month << " " << day << "," << year << "." << endl;
Last edited on
When an exception is thrown, your program exits the try block and looks at the catch block to see if the exception can be handled.

You might want to check the user's input right after they enter it, and force them to enter it again. From your code, it looks like the program will exit if the user enters incorrect input.

In this example, it doesn't look like you are using classes (unless there is another part of your code that defines the class).

You can use exceptions any time - you don't have to use them with other classes.

In this example, you don't need to use exceptions. Just look at the input and see if it is correct. If you want to use exceptions, you can.
You might want to check the user's input right after they enter it, and force them to enter it again. From your code, it looks like the program will exit if the user enters incorrect input.

I want to program to say, for instance, if the user inputs 82 for day then the user should see the message, "The value that you entered is invalid, try again," and for month, if they enter a 13, then get, "The month you entered is invalid, try again."

I thought I was doing it with

1
2
3
4
5
6
7
8
catch (invalidDay)
    {
        cout <<"The value that you entered is invalid, try again."; 
    }
    catch (invalidMonth)
    {
        cout <<"The month you entered is invalid, try again."; 
    }



I'm supposed to use two exceptions classes....the thing is, I don't know how to define them. My book only uses examples with string.


invalidDay {}; and invalidMonth{};

Should I move int day, month, and year into this two classes?
Last edited on
In C++ you can throw anything. A class is commonly used to store info about the exception.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

class Exception
{
    //Your stuff here
}

int main()
{
    try
    {
        Exception ex;
        throw ex;
    }
    catch(Exception e)
    {
        std::cout << "Exception caught" << std::end;
    }
    return 0;
}
so I should get rid of
1
2
3

void inputDate (int) throw (int day, invalidDay, invalidMonth);



in my program?


That depends on how you want to structure your program.
You're not using it now, so you can delete it and add it back if you feel more comfortable splitting the logic in different functions.
C++ also comes with a few standard exceptions (runtime_error, logic_error, etc).
Check out this link if you want to read up on C++ standard exceptions: http://www.cplusplus.com/reference/stdexcept/
These don't store a lot of information, just a string that describes what happened that caused the exception to be thrown.
If you want to use that void function, you can just use it to ask the user for input, but you can also do all of that from main.

You can either define your own exception class, or use the standard ones in C++.
But as maeriden said, you can throw anything in C++. As long as there is some code to catch the exception, C++ doesn't care. But if you want the user to be able to tell what happened, then you might want to define an exception class to store the information. maeriden gave a good example for an exception class. You could have a member variable that is a string that just describes what happened. Usually that's all the user would need to know.
Topic archived. No new replies allowed.