Question: The problem is to write a new exception class named DigitalTimeException and to extend the DigitalTime class as implemented in previous assignments so that it

The problem is to write a new exception class named DigitalTimeException and to extend the DigitalTime class as implemented in previous assignments so that it throws exceptions of type DigitalTimeException when it encounters error conditions. We will implement these as separately compilable files (although I don't care if you separate these into library projects or not, compiling them all in a single project will be fine). You will submit four files to for this problem:

Modified DigitalTime.h

Modified DigitalTime.cpp

DigitalTimeException.h

DigitalTimeException.cpp

Please do the following. First write a new class named DigitalTimeException that has these members:

A constructor that accepts two arguments: an integer error number and a textual error message. Assume that any integer value and any text string will be valid.

Private member variables to store the error number and the error message. Store the error message as a string type variable (use the StudentException class from above as a guide if you need to).

Public member functions errorNumber and errorMessage that return the stored error number and error message, respectively.

Here is an example of creating a DigitalTimeException object:

DigitalTimeException thisError(101, "Something is completely messed up!");

The value of thisError.errorNumber() would be 101 while thisError.errorMessage would return "Something is completely messed up!". Implement this class as DigitalTimeException.h and DigitalTimeException.cpp, just as we learned in the previous lesson.

Second, modify the provided DigitalTime class as follows:

Start with the version of the DigitalTime class as provided in the above links (so that we all use the same version).

Modify the class implementation so that it makes no use at all of cout to display error messages to the user (it will of course still use cout in the overloaded

Replace any existing displays of error messages and any calls to the exit function with Throw statements that throw an exception of type DigitalTimeException. Choose any values you like for the error numbers and error message.

Only submit your modified DigitalTime.h and DigitalTime.cpp files. You will very likely want to test your classes with a test program, but I don't want your test program submitted. I will test your classes with my own test program that forces a number of exceptions. Your job is to provide classes that some unknown to-be-written program will use.

Recovering From Errors

One thing that will make debugging this more easy will be to write your code so that it doesn't have to terminate when an Exception is thrown. If a Thrown exception is properly Caught, the code after the Catch block can continue. Note in our textbook examples how the final two lines of code, after the Catch blocks, executes to properly close the program. That's an example of code that is continuing after an error has occurred. We could do more than just exit the program, we could continue on with any legal programming.

If you include your Try and Catch blocks in a larger block of code, such as the main function, or even inside a block of code within an IF, FOR or WHILE, your code will continue on after the Catch blocks, as long as you have truly caught the error. For example, your test program on the second problem could have a structure something like this:

int main() { try { // Code that forces the first error goes here } catch(DigitalTimeException thisError) { // Code to handle the error }

try { // Code that forces a second error goes here } catch(DigitalTimeException thisError) { // Code to handle the first error }

try { // Code that forces a third error goes here } catch(DigitalTimeException thisError) { // Code to handle the first error } // End of test program }

As long as we Catch any errors generated by a Try block, we have effectively erased the error condition and our program can continue. Here is the output of a simple test program, much like the one you will want to write, that uses two Try/Catch block combinations to test the catching of two errors:

Testing the handling of exceptions generated by DigitalTime objects First try to declare a DigitalTime object with bad parameters Exception #100 encountered: Invalid Hour or Minute Value Now try catching bad inputs to the overloaded operator Please enter an invalid time to test handling that exception: 12:xx Exception #101 encountered: Invalid char acters enter ed to read-mi nute End of Exception Handling Testing Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!