Lecture 17OutlineError Handling 1Error Handling 2ExceptionsThrowing an Exception 1Throwing an Exception 2Handling an Exception 1Handling an Exception 2Handling an Exception 3Handling an Exception 4Managing Complex Projects 1Managing Complex Projects 2Managing Complex Projects 3Managing Complex Projects 4Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 1Lecture 17Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture17/*.*Questions?Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 2OutlineNote: most of the material in this lecture is not in the textbookError handlingExceptions and exception handling (some of this material is in Appendix L of the textbook)throw operatortry-catch blockManaging complex projectsFriday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 3Error HandlingAll functions have a pre-condition that says under what assumptions are made about the arguments passed the function in order for the function to compute a valid result. What should a function do when the arguments to not meet the precondition?Could return an invalid result. I.e., user is solely responsible for using the function correctly.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 4Error HandlingCould cause the program to exit. E.g., the Rational constructor from Project 2 uses assert. Good for errors that cannot be remedied.Could set an error flag or return an error code. E.g., Rational operator<< sets the output stream to a failed state that can be tested. Or Document SetCurrent returns false if used incorrectly. Works but the user can ignore the error.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 5ExceptionsWant a system that forces a user to know there was an error and also allow the user to remedy the problem. See file: except-example.cppAn exception is an object that contains information and is transmitted back to a function caller without using a return.The C++ <stdexcept> library defines a number of classes used for system exceptions that programmers also can use. For this example, we will use the out_of_range class.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 6Throwing an ExceptionAn exception is thrown when an error occurs.In C++, exceptions are thrown using the throw operator. Usually the exception is a newly constructed object. In our example, if the function PickNumber is not called with a positive integer, it throws an out_of_range exception that is constructed with a string error message.// check if called with valid argument// if not, construct and throw exceptionif (upper <= 0) throw out_of_range("Bound is not > 0.");Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 7Throwing an ExceptionWhen an exception is thrown, the function terminates immediately and control transfers back to the caller like a return.Unlike a return, instead of going back to the point of the call, the system searches for an exception handler (code to handle the exception).Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 8Handling an ExceptionAn exception is handled by catching it and providing code to handle the error.In C++, this is done by putting the call to a function that may throw an exception in a try block with a catch block that receives the thrown exception.try { // call a function that throws}catch (const <exception type> & e){ // code to handle the exception}Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 9Handling an ExceptionIf the function does not throw an exception, the catch block is skipped. If the function throws an exception and a catch block parameter matches the thrown exception type, the handler code is executed, then execution continues on after the catch block.There may be more than one catch block attached to a try block. This allows a try block to contain multiple function calls that may throw different types of exceptions.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 10Handling an ExceptionIf there is no catch block with a parameter that matches the thrown exception, the calling function is terminated and the exception is rethrown to its caller. This is repeated until a handler is found.If the main program is reached and no handler is found for the exception, the program terminates with an unhandled exception error.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 11Handling an ExceptionThe example program shows one typical use for exceptions. The try-catch block is placed inside a while-loop that asks the user for input until the input is valid.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 12Managing Complex ProjectsHandout on strategies for managing complex projects and how to use gdb symbolic debugger.0. First and foremost, spend serious time doing analysis and design. Analyses are given in this class, but think about what you want each operation to do before writing any code.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 13Managing Complex Projects1. Implement a piece at a time. Start with an empty class definition with just the attributes. Write the first operation prototype in the class definition, then the implementation for that operation, then a test for that operation in a driver program.Start with a basic constructor and an output operation. Don't worry about complex output formatting until later. Then do the accessors, then the mutators. Don't worry about special cases or error checking at first.Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 14Managing Complex Projects2. Debugging output can be controlled using debug functions. Examine files: rational.h, rational.cpp, rationaltest.cpp. (Skeleton files from Project 2.)void PrintDebug (bool debug, const string & message){ if (debug) cerr << message << endl;}Use boolean constants to control outputconst bool DEBUG1 = true;PrintDebug (DEBUG1, "Entering constructor");Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 15Managing Complex Projects3. Use simple test cases first, then think about boundary cases.4. Sometimes it is better to start over. Usually you have a much better idea of where you want to go, so you get there faster.5.
View Full Document