Documente Academic
Documente Profesional
Documente Cultură
Friends and
Exception Handling
Computer Programming II
Outlines
Friendship
Friend Function
Friend Class
Exception Handling
Exception
Exception Handler
try, throw and catch block
Exception Propagation
Multiple catch Blocks
Exception Matching
Advantages of Exception Handling
Computer Programming II
Friendship
Friendship allows a class to selectively grant
other function or class the access to its private
and protected members.
A friend function is a function that can access
private and protected members of a class, even
though the function itself is not a member of the
class.
A friend class is a class that can access private
and protected members of another class
Computer Programming II
Friendship
Friendship is granted. So for a function A or
class B to access the private and protected
members of class C, C must grant A and B the
friendship
Friendship is not automatically bi-directional.
When A makes B a friend, B does not
automatically make A a friend
Computer Programming II
Computer Programming II
Computer Programming II
Grant tellSecret()
friendship.
tellSecret() is not a
member but friend
Computer Programming II
int main() {
Human h("I have 2
gf:P\n");
Parrot b;
b.tellSecret(h);
return 0;
}
Error. Non-member
cannot access private
member.
Computer Programming II
int main() {
Human h("I have 2 gf:P\n");
Parrot b;
b.tellSecret(h);
return 0;
}
Output:
I have 2 gf:P
Exception Handling
Computer Programming II
Exception
When a program is executed, unexpected situation may
occur. Such a situation is called an exception
In other word: Exception is a runtime error caused by
some abnormal conditions
Example:
division by zero
failure of new operator to obtain a requested amount of
memory
Exception handler is code that handles the exception
(runtime error) when it occurs
Computer Programming II
10
Computer Programming II
11
Computer Programming II
12
Exception Handling
C++ implements exception handling using try, throw
and catch block
try block:
-Write the code that might generate runtime error within
the try block.
Format:
try {
// Code that may generate
// exceptions
}
Computer Programming II
13
try {
...
throw <object>;
...
}
Computer Programming II
14
try {
...
throw <object>;
...
}
catch (<Type of Exception>) {
error handling code;
}
Computer Programming II
15
Computer Programming II
If there is an exception,
throw it
Put code that may
generate error in
try block
If there is no exception,
resume execution
If there is an exception
of type double,
catch it
16
Computer Programming II
Output1:No exception
1 2
Result = 0.5
Output2:With exception
1 0
Cannot divide by zero
Computer Programming II
Output1:No exception
1 2
Result = 0.5
Output2:With exception
1 0
Cannot divide by zero
When an exception is
thrown, the codes
that appear after the
throw statement
won't be executed
18
Computer Programming II
Output1:No exception
1 2
Result = 0.5
Output2:With exception
1 0
Cannot divide by zero
19
Exception Propagation
If the function/method containing the throw statement
does not catch the exception, the exception will be
propagated up to the caller of the function until it
reaches a try block or the main function.
In the former case, the try/catch block of the caller
handles the exception if the exception type matches
one of the catch block. Otherwise the exception will be
propagated up again.
If the exception has reached the main function and is
not handled, the program will be terminated.
Computer Programming II
20
Computer Programming II
Output:With exception
1 0
Cannot divide by zero
The exception is
propagated in the
following order:
divide2(),
divide1(),
divide(),
main()
The main() catches
and handles the
exception
21
22
23
Computer Programming II
int main () {
func (1);
func (2);
func (4);
return 0;
}
Output:
Catch an int argument
Catch a char argument
n is not 1, 2 or 3
Exception Matching
To catch every possible exception type, use ellipsis ""
try {
}
catch (...) { // catches all exceptions
}
Computer Programming II
25
Exception Matching
void func (int n) {
try {
if (n == 1) throw 11; // int
if (n == 2) throw 'a'; // char
if (n == 3) throw 3.5; // double
cout << "n is not 1, 2 or 3\n";
}
catch (int a) {
cout << "Catch an int argument\n";
}
catch (char a) {
cout << "Catch a char argument\n";
}
catch (...) { // all types
cout << "Not int nor char\n";
}
}
Computer Programming II
int main () {
func (1);
func (2);
func (3);
func (4);
return 0;
}
Output:
Catch an int argument
Catch a char argument
Not int nor char
n is not 1, 2 or 3
26
Computer Programming II
27