2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles Catch (exceptionType variable) { //code handles } //code to continue processing //executed unless catch block stops processing with halt; or return
Example … try { infile >> value; if (value < 0 ) throw string (“Negative value”); y = sqrt (value); } catch (string message ) { //code handles cout << message << “found in file. Abort.”; return –1 } //code to continue if exception not thrown cout << “sqrt of “ << value << “is “ << y << endl;
Types to throw and catch n strings – for messages n ints – to use in a case to handle different reasons exception thrown
Specifying Functions n Up to now Purpose/Receives/Returns n Text Book uses Pre- and Post-conditions n Pre-condition: describes what must be true about the state for the function to work correctly n Post-condition: describes what will be true about the state if the pre-conditions are met
Specifying Functions cont. n Precondition – discusses appropriate values for receives parameters and member data of object if it is a member function we are specifying n Post-condition – describes what the return variables values will be and what changes will be made to the object
Error Conditions Describe what preconditions will be checked by the function (might use try/catch)
Primary vs Enhanced Operations n Primary -Member functions – Constructor – Transformers n Change 1 piece of ADT n Add 1 piece to ADT n Delete 1 piece of ADT – Observers n Accessors – look at 1 piece of ADT n Predicates – return a bool value regarding state of ADT – Destructor n Enhanced - Non-member functions – manipulate many pieces of ADT (iterators) – manipulate several pieces of ADT – Manipulate several instances of same type of ADT – Call primary operations to get work done
#ifndef n #ifndef FILE_H n #define FILE_H n #endif