cs Oct 7
Goal : finish exception handling (ch5)
if time start chapter 9 , read all of ch 9 by next week, next quiz (after 5) is 9
int num = 1;
int denom = 0;
if (denom !=0)
cout << num/denom; (handling the error
exception handling : added to C++ baecause" separate detection of errors from handling of errors why ? --> the code that detects the error may not know the best way to handle it. eg.. display device being available. also to reuse code.
may be in a library of classes or functions written long before the application is written.
range_error is not thrown by default for out of range errors on subscript operators unless std_lib_facilities.h is used.
if you want the exception thrown use at function. v.at[2] as opposed to v[2] (which doesn't throw)
try {
}
catch (out_of_range&) {} // out_of_range errors
catch(...) {} // all other errors
throw 0; // what is the type of the value 0 (int, so would be caught by ...
vector<int> v(3);
reference to v[10] would not throw an out of range error but v.at[10] would throw an out of range error.
why does he use a struct sometimes and a class sometimes. He uses class instead of struct if there is a meaningful class invariant. if there is no invariant then uses a struct instead of a class i.e. members can take any values or no constraints.
eg. a class for date (months could be only from Jan to Dec)
out_of_range : built in to standard library.
data members in range_error class : index..
function in range_error class : constructor function.
class Bad_area{}; // do data member and member functions.. creates a default constructor.
throw Bad_area() ; //creating a type.
catch(Bad_area)
postcondition would return the area. which should be positive so that may be checked...

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home