CS120 - Day 16: Exceptions & Teamwork --------------------------------------- TEAMWORK --------------------- Pair Programming: - driver/navigator roles - take turns in roles - highly recommended for better development Good, - communication - multiple perspectives & ideas - share work according to abilities - planning - asking for help - sharing code/work (commit early, commit often) - compromise - try several, pick best - take turns - find middle ground Bad & - can't agree on things - scheduling work with partners - people don't pull their weight - time management & expectations - integrating code Ugly in Teamwork: - one person does all the work - procrastinating, not getting things done Make BitBucket Project Repositories! - one team member makes/owns it - create it private - give access to team members, Joanne & James ERROR HANDLING ----------------------------------------- - exit(int) - must #include - to cleanly and gracefully exit a program immediately - will flush buffers and close files - will execute destructors for static objects - int is a return code for the op sys - 0 indicates no errors, non-zero indicates some error - no return to main, immediate quit of program run - abort() - end program, no clean-up (no destructors, etc.) - assert(condition) - must #include - ignored if NDEBUG is defined - if true, nothing happens - if false, program stops abruptly (aborts), prints file/line# assert message - no clean-up, flushing, etc. performed - examples: Point *parray; parray = new Point[6]; if (parray == 0) { cerr << "not enough memory for point array"; exit(1); } assert(parray != 0); EXCEPTIONS ---------------------------------------------------------- - an exception is a value that indicates a problem - it can be of any data type - use for memory exhaustion, subscript range errors, division by 0 - program *throws* exception where the error first occurs - C++ searches for code called exception handler - responds by catching the exception, otherwise program terminates - make class for the exception (maybe empty) - put code which throws the exception (nested) in a try block - put code to catch and report the exception after the try block - can rethrow the exception further out in the catch handler - will search outward from where exception is thrown to be caught - unwinds the stack by destroying objects created since it entered the try block - destructors for local objects in function throwing except are called - calls std::terminate which calls std::abort if reaches main without being caught - memory exhaustion automatically throws bad_alloc exception "try" block put around code that will eventually throw an exception "catch" phrase for each exception you want to handle follows try block put handling code can further throw the exception that you caught "throw" exception object where you find an error or problem If try block throws exception not caught, std::terminate called (which calls abort if main is reached) >>> EXAMPLE: Bank classes (inheritance example too) <<<< Declare which exceptions a method can throw in the header: void func(int param) throw (char, domain_error) void func(char thing) throw() // means can't throw any void func() // no throw means can throw anything - list of exceptions thrown by function - not checked at compile time - if unlisted exception is thrown, std::unexpected called - put on prototype and function definition - if listed exception is thrown but not caught, std::terminate - missing throw list (as opposed to empty) means any exception may be thrown Class inheritance hierarchy for standard exception classes: exception bad_alloc, bad_cast, bad_typeid, bad_exception, ios_base::failure logic_error domain_error, invalid_argument, length_error, out_of_range runtime-error range_error, overflow_error, underflow_error - every exception object has message (const char *): e.what() - constructors for exceptions have string argument (for message) - can derive your own exception classes - catch parameters must be passed by reference (or pointers) to take advantage of inheritance - 'throw' the exception further out as part of the catch code catch (const std::range_error & e) { cout << "range error" << endl; cout << e.what() << flush; throw; // throws the one that was caught } - catch (...) for leftovers