Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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;

3 Types to throw and catch n strings – for messages n ints – to use in a case to handle different reasons exception thrown

4 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

5 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

6 Error Conditions Describe what preconditions will be checked by the function (might use try/catch)

7 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

8 #ifndef n #ifndef FILE_H n #define FILE_H n #endif


Download ppt "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."

Similar presentations


Ads by Google