11602105 C Lecture 12 Friends Exception Handling

  • Uploaded by: booksofpavan
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 11602105 C Lecture 12 Friends Exception Handling as PDF for free.

More details

  • Words: 1,367
  • Pages: 26
Lecturer 12

Friends and Exception Handling

Computer Programming II

1

Outlines        

Friend relationship Friend function Friend method Friend class Exception Handling Exception Exception Handler try, throw and catch block Computer Programming II

2

Access Privileges - Review (private, Public, protected) 1. Public: methods of class itself, methods of derived class, and other functions like main can access it. 2. Protected: methods of class itself and methods of derived class can access it. 3. Private: methods of class itself can access it.

Computer Programming II

3 3

Will you be my friend? • Friend-ship allows a class to selectively grant access to its private internals to other constructs. • C++ friends come in three flavors: • friend functions • friend classes • friend methods (member functions) • Friend-ship is • A friend function can access both the private and protected members of the class. Computer Programming II

4

Friends function/method/class

• Friend-ship allows a class to selectively grant access to its private and protected internals to other function, method, and class. • C++ friends come in three flavors: • friend functions • friend classes • friend methods (member functions) - A friend function is a function that can access private and protected data members of a class, even though the function itself is not a member of the class. - A friend class is a class whose functions can all access private and protected data members of another class - A friend method is a method that can access private and protected data members of a class, even though the method itself is not a member of the class.

Computer Programming II

5

Example: Friend Function / Method class Chong {… public: Chong(); // constructor friend void perform_sthg( ); ... }; … void perform_sthg() { // can access private&Protected data of //Chong ... }

class Yin {… public: Yin(); // constructor int Yin_f1(); ... // can access private&Protected }; //data of Lim class Lim { public: Lim(); // constructor friend int Yin::Yin_f1(); ... };

The functions perform_sthg has access to private&Protected data of Chong

Only the member method Yin_f1 has access to private&Protected data of Lim

Computer Programming II

6

Example: Friend Class class Raj {… public: Raj(); // constructor int Raj_f1(); int Raj_f2(); int Raj_f3(); }; class Singh {… public: Singh(); // constructor friend class Raj; ... };

Computer Programming II

// can access private&Protected data of Singh

All member methods of Raj (f1, f2, f3) have access to Private&protected data of Singh

7

Friend Function

Computer Programming II

8

Friend Method

Computer Programming II

9

Friend Class Friendship is not inherited, meaning that classes derived from YourOtherClass cannot access YourClass's private members. Friendship is not transitive, so classes that are friends of YourOtherClass cannot access YourClass's private members.

Computer Programming II

10

Computer Programming II

11

Exception Handling

Computer Programming II

12

Dealing with Errors When a program is executed, unexpected situation may occur. Such a situation is called an exception In other word: Exception is a run-time error caused by some abnormal condition

Example: • indexing outside the limits in an array • division by zero • failure of new to obtain a requested amount of memory

Exception handler • it’s a code to deal with these exceptions (run-time errors) when they occured

Computer Programming II

13

Exception Example

Division by zero

Index out of range

float x, y; …. y = 0.0; float result = x/y

int array [10]; for (int i=0; i<=10; i++) array [i] = something;

How to deal with these exceptions? Computer Programming II

14

Dealing with Errors Three aspects of dealing with an error: 1. Identifying that an error has occurred 2. Handling (resolving) an identified error in order to either continue execution of the program OR gracefully terminate it. 3. If the error cannot be resolved at the current level of execution, communicate its existence to the next highest level of execution int main() { int x,y; cout << “Enter for two numbers;” cin >> x >> y; cout << “The first num divided by the second is: “<< MyDivide(x, y) << endl; } double MyDivide(double numerator, double  denominator) { return numerator / denominator; Error if denominator=0!!! }

Computer Programming II

15

Introduction to Exceptions • An exception is an object (int, char, text..etc) that contains information on an error and is used to communicate that information to a higher level of execution. • The process of transferring an exception from its current execution context to the context above it is knows as throwing an exception • The point in the function code at which the exception is thrown is referred to as the throw point • An execution context that receives an exception and accesses it is said to catch the exception

Computer Programming II

16

The Syntax of Exceptions C++ implements exceptions with three keywords try, throw and catch Try Block - The code which might generate a runtime error is written within the try block. Format: try { //Code that can generate exceptions } Computer Programming II

17

The Syntax of Exceptions throw - An exception is generated by using keyword throw. That is, to signal that abnormal condition has occurred. Format: throw ; “” is to be replaced with type of object to be thrown.

Example:

throw 15;

Computer Programming II

//throws int 15 as an exception

18

The Syntax of Exceptions Catch Block - Catches the exception (errors) which is thrown by throw keyword which is also generated from the code within the try block. This is the exception handler. Format: 5.catch() {error handling part:} OR 2. catch () { error handling part; } Computer Programming II

19

# include int main() { int value1, value2, result; try { cin >> value1;

cin >> value2;

if (value2 == 0) { throw value2; } result = value1/value2; cout <<"result is :"<< result; }// end of try

We write the code in a try block

If there is an exception, we throw it to a handler If there is no exception, we resume the execution

catch (int a ) { cout << " just cannot divide by zero. Value2= “<
Computer Programming II

20

The try-catch block Some times, we might have many different exceptions 1. We should write as many catch blocks. 2. This means also that we should have as many throw statements. 3. BUT(usually), only one try. But, which catch block will be instigated? (invoked) The conflict will be eliminated depending on the parameters in the throw, i.e., OVERLOADING

Computer Programming II

21

The try-catch block try { // Code that could generate an exception    } catch (<Exception type1 rel="nofollow">) { // Code that resolves a type1 exception } catch (<Exception type2>) { // Code that resolves a type2 exception }… catch (<Exception typeN>) { // Code that resolves a typeN exception };  

// end­of try­catch block

Computer Programming II

22

The try-catch block

Computer Programming II

23

Exception Matching • To catch every exception that is seen, use ellipsis. catch (…)

//This catches any exception

• You can't tell what type of exception occurred • No argument to reference • Must always be placed as the last catch

Computer Programming II

24

Exception Matching Catch all void func (int n) // throw (double,int,char) { int a[10]; i; try { if(n==11) throw 103.02; if(n>11) throw 10; if(n<=0) throw ‘a’; for(I=0;I<10;I++) cin>>a[i]; } catch (int n) { cout<<“catch with int as argument”; }

Computer Programming II

catch(…) { cout<<“In catch all”; } } void main ( ) { int n; cout<<“Enter # of elements”; cin>>n: Output: func(n); Enter # of elements 12 catch with int as argument Enter # of elements 0 In catch all

25

Exception Matching Throwing Exception from Function void main ( ) { int a[10], n, I; cout<<“enter # of elements”; cin>>n: func(n); for(I=0;I>a[I]; int sum=0; for(I=0;I
void func (int n) { try { if(n>10) throw 10; } catch (int i) { cout<<“Array out of bounds”; cout<
Computer Programming II Enter the number of elements Array out of bounds

10

11 26

Related Documents


More Documents from ""