Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
The topics discussed in this chapter are: Object creation - a detailed analysis, assigning a reference, testing objects for equality, passing references as arguments, method-call chaining, overloaded methods, constructors, overloaded constructors. | CSC241: Object Oriented Programming Lecture No 27 Previous Lecture Functional templates Example program multiple arguments Macros vs. template Class templates Stack template class Default and non type parameter for class template Explicit specialization Today’s Lecture Exception Exception handling try , catch and throw block Multiple exceptions Exceptions Exceptions are errors that occur at runtime Exception handling provide a systematic, object-oriented approach to handling errors generated by C++ classes For example, running out of memory, not being able to open a file, trying to initialize an object to an impossible value, or using an out-of-bounds index Why Do We Need Exception handling C-language programs signal an error by returning a value from the function For example, disk-file functions often return NULL or 0 to signal an error if( somefunc() == ERROR_RETURN_VALUE ) //handle the error else //proceed normally if( anotherfunc() == NULL ) //handle the error else //proceed . | CSC241: Object Oriented Programming Lecture No 27 Previous Lecture Functional templates Example program multiple arguments Macros vs. template Class templates Stack template class Default and non type parameter for class template Explicit specialization Today’s Lecture Exception Exception handling try , catch and throw block Multiple exceptions Exceptions Exceptions are errors that occur at runtime Exception handling provide a systematic, object-oriented approach to handling errors generated by C++ classes For example, running out of memory, not being able to open a file, trying to initialize an object to an impossible value, or using an out-of-bounds index Why Do We Need Exception handling C-language programs signal an error by returning a value from the function For example, disk-file functions often return NULL or 0 to signal an error if( somefunc() == ERROR_RETURN_VALUE ) //handle the error else //proceed normally if( anotherfunc() == NULL ) //handle the error else //proceed normally if( thirdfunc() == 0 ) //handle the error else //proceed normally Cont. Problem : Every single call to such a function must be examined by the program if else block and writing statement to handle error requires more code and make file size larger More complex problem: classes are used, errors may take place without a function being explicitly called How application know if there is error in constructor SomeClass obj1, obj2, obj3; Exception syntax Imagine an application that creates and interacts with objects of a class Usually the application’s calls to the class member functions cause no problems Sometimes, the application makes a mistake, causing an error to be detected in a member function This member function informs the application that an error has occurred When exceptions are used, this is called throwing an exception. Cont. A separate section of code to handle the errors is called exception handler or catch block catch block catches the exceptions thrown by the member .