Exception Haldling

I have been asked to add an exception class to the digital time problem that excepts two arguments an int value and an error message. I think I have a problem with the catch parameters or maybe the throw statement itself. because when my program encounters an error it crashes. Even the catch(...) does nothing. Do I have a another simple mistake that I just don't see? Or is it something bigger?

main()
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
int main()
#include <cstdlib>
#include <iostream>
#include "DigitalTimeException.h"
#include "DigitalTime.h"

using namespace std;

int main()
{
    try
{
    DigitalTime clock, old_clock;
    
    cout << "Enter the time in 24 hour notation (example 15:45)  ";
    cin >> clock;
    old_clock = clock;
    clock.advance(15);
   
}
    catch ( DigitalTimeException& e)
{
  cerr << e.errorNumber() << " : " << e.errorMessage() << endl;
}
catch(...)
{
  cout << "undefined error occured" << endl;
}
    system("pause");
    return 0;
}

}


Digital Time Exception.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include "DigitalTimeException.h"
using namespace std;

DigitalTimeException::DigitalTimeException(int value, string whatWentWrong)
{
	// Set the stored message within the object
	// Any text or int value will be accepted as the error message
	message = whatWentWrong;
	num = value;
}
// Return the error message stored inside the object
string DigitalTimeException::errorMessage()
{
	return message;
}
// Return the error number stored inside the object
int DigitalTimeException::errorNumber()
{
    return num;
}


the function from dtime.cpp that is supposed to throw the error
1
2
3
4
5
6
7
8
9
10
11
12
13
DigitalTime::DigitalTime(int the_hour, int the_minute)
{
	if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59)
	{
     
        throw DigitalTimeException (001, "Illegal argument to DigitalTime constructor.");
	}
	else
	{
		hour = the_hour;
		minute = the_minute;
	}
}


the digital time exception class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef DIGITALTIMEEXCEPTION_H
#define DIGITALTIMEEXCEPTION_H
#include <string>
using namespace std;

class DigitalTimeException
{
public:
    DigitalTimeException(int errorNumber, string ErrorMessage);
    string errorMessage();
    int errorNumber();
private:
    string message;
    int num;
};
#endif


as always any help at all is greatly appreciated.
All of this works, something else must be going on.
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
#include <cstdlib>
#include <iostream>
#include <string>
class DigitalTimeException {
   public:
      DigitalTimeException(int errorNumber, std::string ErrorMessage){
          num = errorNumber;
          message = ErrorMessage;
      }
      std::string errorMessage(){
          return message;
      }
      int errorNumber(){
          return num;
      }
   private:
      std::string message;
      int num;
};

class DigitalTime {
   public:
      DigitalTime(int the_hour, int the_minute) {
         if (the_hour < 0 || the_hour > 23 ||
               the_minute < 0 || the_minute > 59) {
             throw DigitalTimeException (001,
                      "Illegal argument to DigitalTime constructor.");
          } else {
             hour = the_hour;
             minute = the_minute;
          }
      }
   private:
      int hour,minute;
};

int main() {
    try {
       //---------------------------------------------
       // This works!
       DigitalTime clock(400,9);
       //cout << "Enter the time in 24 hour notation (example 15:45)  ";

       //---------------------------------------------
       // Were is the code for the below
       //cin >> clock;
       //old_clock = clock;
       //clock.advance(15);
    } catch ( DigitalTimeException& e) {
       std::cerr << e.errorNumber() << " : " << e.errorMessage() << std::endl;
    } catch(...) {
       std::cout << "undefined error occured" << std::endl;
    }
    return 0;
}
$ ./a.out
1 : Illegal argument to DigitalTime constructor.
I just figured out the problem. The other functions that take DigitalTime as an input still had the if statements and exit sequence. I changed them to throw statements and now it works. I was under the impression that I could change the functions one at a time and test them as I go. But after thinking about it for a moment it makes sense why I cant do that.
Topic archived. No new replies allowed.