Lecture 17 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture17/*.* Questions? Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 1 Outline Note: most of the material in this lecture is not in the textbook Error handling Exceptions and exception handling (some of this material is in Appendix L of the textbook) throw operator try-catch block Managing complex projects Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 2 Error Handling All 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 3 Error Handling Could 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 4 Exceptions Want 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.cpp An 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 5 Throwing an Exception An 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 exception if (upper <= 0) throw out_of_range("Bound is not > 0."); Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 6 Throwing an Exception When 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 7 Handling an Exception An 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 8 Handling an Exception If 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 9 Handling an Exception If 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 10 Handling an Exception The 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 11 Managing Complex Projects Handout 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 12 Managing Complex Projects 1. 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 13 Managing Complex Projects 2. 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 output const bool DEBUG1 = true; PrintDebug (DEBUG1, "Entering constructor"); Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 14 Managing Complex Projects 3. 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. Last, but not least, use a symbolic debugger to trace code. For Linux/UNIX, use gdb. There are several GUI front-ends, but the commandline version works well and can be used in any terminal. Compile files with -g option. Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 15