DOC PREVIEW
UE CS 215 - Lecture 17

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 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 2Outline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 projectsFriday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 3Error 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 4Error 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 5Exceptions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 6Throwing 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 exceptionif (upper <= 0) throw out_of_range("Bound is not > 0.");Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 7Throwing 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 8Handling 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 9Handling 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 10Handling 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 11Handling 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 12Managing 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 13Managing 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 14Managing 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 outputconst bool DEBUG1 = true;PrintDebug (DEBUG1, "Entering constructor");Friday, February 18 CS 215 Fundamentals of Programming II - Lecture 17 15Managing 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.


View Full Document

UE CS 215 - Lecture 17

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download Lecture 17
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 17 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 17 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?