Lecture 18OutlineError 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 4Managing Complex Projects 5Managing Complex Projects 6Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 1Lecture 18Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture18/*.*Reminders: Project 3 and Homework 5 due today. No class on Friday.Midterm (written) exam next week. Will Wednesday, October 13, 5-7pm work? Review this Wednesday. Review sheet posted; sample problems handed out.Project 5 posted; due October 20. Do not wait until after fall break to start! Material on exam.Questions?Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 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 projects, i.e. Project 4Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 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.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 4Error HandlingCould cause the program to exit. E.g., the Date constructor from Project 2. Good for errors that cannot be remedied.Could set an error flag or return an error code. E.g. the Date operator<< sets the output stream to a failed state that can be tested. Works but the user can ignore the error.Want a system that forces user to know there was an error and also allow the user to remedy the problem. Use exceptions.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 5ExceptionsExamine file: except-example.cppAn exception is an object that contains information and is transmitted back to a function caller without the using a return.The C++ <stdexcept> library defines a number of classes used for system exceptions that programmers also can use. For this example and Project 4, we will use the out_of_range class.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 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.");Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 7Throwing an ExceptionWhen an exception is thrown, a 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).Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 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 (<exception type> & e){ // code to handle the exception}Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 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.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 10Handling an ExceptionIf there is no catch block with a parameter that matches the thrown exception, the calling function is terminated and the exception 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.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 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.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 12Managing Complex ProjectsProject 4 is to implement a text-based Minesweeper game as specified.Use of SweeperCell class that is givenDynamic allocation of grid to user-specified size during constructionNo reallocation during operations, but need destructor, copy constructor, and assignment operatorExtra credit opportunitiesMonday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 13Managing Complex ProjectsHandout on strategies for managing complex projects and how to use gdb symbolic debugger.Examine files: date.h, date.cpp, datetest.cpp Skeleton Date class and autotest program from Project 2.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.Monday, October 4 CS 215 Fundamentals of Programming II - Lecture 18 14Managing 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
View Full Document