tailieunhanh - Lecture Object oriented programming - Lecture no 28
This chapter presents the following content: Class Variables, class methods, how to access class members, when to use class members, class constants, example program using class members. | CSC241: Object Oriented Programming Lecture No 28 Previous Lecture Exception Exception handling Why exception handling ? try , catch and throw block Program skeleton Example program stack ( class Range { }; ) Multiple exceptions class Full { }; class Empty { }; Today’s Lecture Exception in Distance class Re-throwing an exception Exception with arguments bad_alloc class set_new_handler function class Distance { private: int feet; float inches; public: class InchesEx { }; Distance() { feet = 0; inches = ; } Distance(int ft, float in) { if(in >= ) throw InchesEx(); feet = ft; inches = in; } void getdist() { cout > feet; cout > inches; if(inches >= ) throw InchesEx(); } }; Exceptions with the Distance Class Go to program Re-throwing an Exception An exception handler (catch block) when receive an exception may decide that It cannot process that exception or It can process the exception only partially It might re-throw an . | CSC241: Object Oriented Programming Lecture No 28 Previous Lecture Exception Exception handling Why exception handling ? try , catch and throw block Program skeleton Example program stack ( class Range { }; ) Multiple exceptions class Full { }; class Empty { }; Today’s Lecture Exception in Distance class Re-throwing an exception Exception with arguments bad_alloc class set_new_handler function class Distance { private: int feet; float inches; public: class InchesEx { }; Distance() { feet = 0; inches = ; } Distance(int ft, float in) { if(in >= ) throw InchesEx(); feet = ft; inches = in; } void getdist() { cout > feet; cout > inches; if(inches >= ) throw InchesEx(); } }; Exceptions with the Distance Class Go to program Re-throwing an Exception An exception handler (catch block) when receive an exception may decide that It cannot process that exception or It can process the exception only partially It might re-throw an exception to another exception handler via throw statement Go to program Exceptions with Arguments What happens if the application needs more information about what caused an exception? For example In the Distance example, it is useful to display what the bad inches value actually was If the same exception is thrown different member functions, it be useful to display which function causes that exception to be thrown Two things happened when an exception is thrown transferring control to the handler (catch block) creating an object of the exception class by calling its constructor throw Full(); throw Empty(); class Distance { private: int feet; float inches; public: class InchesEx { public: string origin; float iValue; InchesEx(string or, float in) { origin = or; iValue = in; } }; Distance() { feet = 0; inches = ; } . . . Exceptions with Arguments-Distance class main() { try{ Distance dist1(17, ); Distance dist2; . . . . } catch(Distance::InchesEx ix) { cout << “Error in “ <<
đang nạp các trang xem trước