2.4 Exceptions Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } 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 – for messages ints – to use in a case to handle different reasons exception thrown strings Specifying Functions Up to now Purpose/Receives/Returns Text Book uses Pre- and Post-conditions Pre-condition: describes what must be true about the state for the function to work correctly Post-condition: describes what will be true about the state if the pre-conditions are met Specifying Functions cont. – discusses appropriate values for receives parameters and member data of object if it is a member function we are specifying Post-condition – describes what the return variables values will be and what changes will be made to the object Precondition Error Conditions Describe what preconditions will be checked by the function (might use try/catch) Primary vs Enhanced Operations Primary -Member functions – – Constructor Transformers – Observers – Change 1 piece of ADT Add 1 piece to ADT Delete 1 piece of ADT Accessors – look at 1 piece of ADT Predicates – return a bool value regarding state of ADT Destructor 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 #ifndef FILE_H #define FILE_H #endif