Difficulty applying exception classes

According to this exercise based on exception handling,
Write a program that prompts the user to enter time in 12:00:00AM thru 11:59:59PM (12-hour notation). The program then outputs the time in 00:00:00 thru 23:59:59 (24 – hour notation). Your program must contain three exception classes: invalidHr, invalidMin, invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalid HR object. Follow
similar conventions for the invalid values of minutes and seconds.

Each exception class (invalidHr, invalidMin, invalidSec) should have 2 constructors:
The default constructor that supplies the standard error message and a parameterized constructor which takes a string variable to initialize the error message.
Your derived exception classes should also supply the necessary overloads for returning an error message.

Now my problem isn't writing the code to output the 24-hour notation code, but it lies within how to code and use exception classes in this particular case. I'm honestly really lost on how to code the exception classes. Any help is appreciated!

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
#include<iostream> 
using namespace std; 
  
void print24(string str) 
{ 
    // Get hours 
    int h1 = (int)str[1] - '0'; 
    int h2 = (int)str[0] - '0'; 
    int hh = (h2 * 10 + h1 % 10); 
  
    // If time is in "AM" 
    if (str[8] == 'A') 
    { 
        if (hh == 12) 
        { 
            cout << "00"; 
            for (int i=2; i <= 7; i++) 
                cout << str[i]; 
        } 
        else
        { 
            for (int i=0; i <= 7; i++) 
                cout << str[i]; 
        } 
    } 
  
    // If time is in "PM" 
    else
    { 
        if (hh == 12) 
        { 
            cout << "12"; 
            for (int i=2; i <= 7; i++) 
                cout << str[i]; 
        } 
        else
        { 
            hh = hh + 12; 
            cout << hh; 
            for (int i=2; i <= 7; i++) 
                cout << str[i]; 
        } 
    } 
} 
  
// Driver code 
int main() 
{ 
   string str = "07:05:45PM"; 
   print24(str); 
   return 0; 
} 
They're new classes, to inherit from std::exception
1
2
3
4
5
6
7
8
9
class invalidHr: public std::exception
{
  // in here don't forget the constructors and functions to do:
  //  Each exception class (invalidHr, invalidMin, invalidSec) should have 2 constructors:
  //  The default constructor that supplies the standard error message and a parameterized constructor which
  // takes a string variable to initialize the error message.
  // Your derived exception classes should also supply the necessary overloads for returning an error message.

};


Last edited on
For a simple example of defining and using an exception class, consider:

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
#include <iostream>
#include <exception>
#include <string>

class myExcept : public std::exception {
public:
	myExcept() {}

	myExcept(const std::string& mess) : message(mess) {}

	virtual const char* what() const noexcept override {
		return message.c_str();
	}

private:
	std::string message {"negative!"};
};

void numcheck(int num)
{
	if (num < 0)
		throw myExcept();

	if (num > 100)
		throw myExcept("Number is greater than 100!");
}

int main()
{
	int num;

	while ((std::cout << "Enter a number: ") && (std::cin >> num) && num != -9999) {
		try {
			numcheck(num);
		}
		catch (const myExcept& e) {
			std::cout << "num exception: " << e.what() << '\n';
		}
	}
}


For using with hour, minute second, the test(s)/messages change for each class. You'll also need to obtain the hour, minute & second as a number to test.

Topic archived. No new replies allowed.