Beruflich Dokumente
Kultur Dokumente
Agenda
A Crash Course on Exceptions Classes Some Additional Features Using Constructors for Casting const Objects and Functions static Variables and Functions Friendship Inner type/class
A Crash Course
on
Exceptions
Exceptions
Common runtime errors issues:
Memory Files Numeric operations I/O
User (Application)
Detection
Handling
copyrights Elhanan Borenstein
Exceptions
Motivation
As there may be certain actions which can cause run-time problems, we need some sort of mechanism to report and handle these potential errors. We can use the return-value mechanism. This mechanism have several problems:
We may forget to check the return value. There may not be an available value we can use. Example!!! We have to check the return value for each and every function separately.
Exceptions
Usage
Exception handling is implemented with 3 keywords:
throw when an error occurs in a function, it will use throw to throw a certain object (of any type). try the problematic function call will be nested within a try block. catch one or more catch blocks should immediately follow the try block. If the function throws an exception, this block will handle it. void FuncA () { . . throw }
void FuncB() { try { FuncA() . } catch () { } }
copyrights Elhanan Borenstein
Example: exm_exc
Exceptions
Exception Handling
If the exception is not caught, it will percolate up, (function after function) until reaching the main (where it will abort).
Conclusions: Somewhere, sometime, someone is going to pay!!!
No casting takes place!!! If more than one catch block fits the exception type, the first will be used. catch () will can catch all types. Should be last!!! An exception can be thrown again (using throw command) A function can declare which exception it may throw: FuncA() or FuncA() throw(int,char*) or FuncA() throw()
copyrights Elhanan Borenstein
Exceptions
Any type can be thrown as exception. It is common to throw objects of a certain class family.
class CExp { public: void Print() { cout<<General Error; } }; class CExpFile { public: void Print() { cout<<File Error; } }; class CExpErrorCode { int Code; public: CExpErrorCode(int a_code) { } void Print() { cout<<Code<<Code; } }; . . . throw CExpErrorCode(23); . . .
catch(CExpErrorCode& e) { e.Print(); } . .
Exceptions
Throwing Exceptions in Ctors and Dtors
Naturally, exceptions are very suitable for constructors (no return value!!!) If the Ctor did not terminate properly (an exception was thrown), the dtor will not be activated. Allocated memory should be released before throwing the exception or in the catch block. More on that when we get to inheritance and polymorphism.
Classes
some additional features
The same is true for a function that expects an object. The casting in this case will be done using the object constructor !!!!!!!!!!! ! ! ! Example: cpoint_cast
The programmer has to indicate which member functions do not change the object data members (using the keyword const after the function name). Only these functions can be activated on a const object. Example: cpoint_const_method
copyrights Elhanan Borenstein
A mutable data member can be changes even if the object is const. Usage??
C++
It can be accessed from any member function of that class. ANDit is shared among all objects/instances of that class.
Example: student2
Friendship Permissions
Friendship
As we recall, private members are accessible only to member functions of that class. The class can, however, allow certain functions or classes to access its private members using the keyword friend. This mechanism should be used wisely as it somewhat violates the concepts of OOP Friend permissions can be given to global functions or classes.
Friendship Permissions
Friend Global Functions
A function can be declared within the class as a friend. This function will be a global function(!!!) and can access private data members. A friend function cannot be defined as const. WHY? Example: cpoint_friend
Friendship Permissions
Friend Classes
A different class can be declared within the class as a friend. This class will be able to access (and change) private data members. Example: cline_friend
Inner Types/Class
A class or an enum can be defined within another class. When will we use inner classes? inner types/class can be defined as either private or public. A public inner class can be used anywhere in the application by using its full name:
ext_class::inner_class ext_class::inner_class::func()
Questions?