<<

Zimmer CSCI 330

Lab 2 - Lab Due: ______Submit: lab2hw.cpp using zimmer330-submit.sh, program name: lab2

Purpose: Understand ++ exception handling:

Exception - A run-time error caused by some abnormal condition. • throwing an exception – a process signals that a run-time error occurred. • catching an exception – code that is written to handle the exception. • try block – the block of code that could cause an exception that needs to be handled. Execution jumps from this block expecting a catch block immediately after.

Complete the following exercise: 1. Read the following on exception handling: http://www.cplusplus.com/doc/tutorial/exceptions/

2. Copy the following files from my account: cp ~zimmer/csci330/lab2/lab2ex.cpp . cp ~zimmer/csci330/lab2/lab2.cpp .

3. Edit all the files you copied inserting a comment with your name and section at the top of each file. Make sure the messages that are added are done exactly as shown (no extra endls). 4. Compile, link and run the program lab2ex.cpp. The program catches any exception thrown by the “at” method of string. Enter a string and valid index position, normal processing occurs. Enter an out of range index and an exception is thrown. 5. Take a look at the code in lab2ex.cpp to see how the exception is handled. 6. Edit lab2.cpp – you will be writing the code to throw an exception and writing the code to catch the exception. Add the code so that the function SmallPosInt( ) will throw an exception if the integer is negative ( < 0). Throw a simple integer value to indicate the error. Compile, link and run the program forcing the exception. It should abort when the exception is thrown. 7. Edit lab2.cpp – Add the try block around all the code in the for-loop. The catch the exception and print out the error message “ is negative”. Your catch block can either have the ellipsis (...) or a parameter that is of type integer. Compile, link and run the program. When the error is forced your program should not abort, but instead print the message “value is negative”. Make sure the message does not print when the data entered is greater than or equal to 0.

1 Zimmer CSCI 330

8. Edit lab2.cpp – Add the code so that the function SmallPosInt( ) can throw another exception if the integer is large ( > 10). The thrown integer value should be different from the one used to indicate a negative value. The catch block will need a parameter of type int to distinguish the error. Print the error message “value is negative” if it is negative, and “value is too large” if value is too large. Compile, link and run the program. When the error is forced your program should not abort, but instead print the appropriate message. Make sure the message does not print when the data is within the bounds. 9. Edit lab2.cpp – Replace the thrown integer with a value of another (string or char). Make the appropriate changes to the catch block so that you can process the thrown error. Compile, link and run the program. When the error is forced your program should not abort, but instead print the appropriate message as indicated in #7. Make sure the message does not print when the data is within the bounds. 10. Copy lab2.cpp into lab2hw.cpp “ cp lab2.cpp lab2hw.cpp

11. Edit lab2hw.cpp – Add an enumerated type for your exception handling and a corresponding string array of error messages to the top of the program: enum ExType {NEGATIVE, TOO_LARGE, END_EXTYPE}; const string ERR[END_EXTYPE] = {“value is negative”, “value is too large”}; Replace the original thrown values used in lab2.cpp with the appropriate enumerated value. Rewrite the catch block to print the corresponding ERR string based on the enumerated value thrown. Make sure when NEGATIVE is thrown the message “value is negative” is output and when TOO_LARGE is thrown the message “value is too large” is output. 12. Be sure your name is at the top of lab2hw.cpp and submit it using zimmer330-submit.sh and program name: lab2 13. Further study of exception handling can be done by reading: Chapter 14 of your textbook. 14. We will be adding an enumerated type and error strings to GameSpace so we can handle construction errors and other errors in our programs moving forward. Please take a look at the new enumerated type in gamespace.h.

2