11602105 C Lecture 12 Friends Exception Handling

Embed Size (px)

Citation preview

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    1/26

    Computer Programming II 1

    Lecturer 12Lecturer 12

    Friends andFriends andException HandlingException Handling

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    2/26

    Computer Programming II 2

    OutlinesOutlines

    Friend relationshipFriend relationship Friend function

    Friend method

    Friend class

    Exception HandlingException Handling Exception

    Exception Handler try, throw and catch block

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    3/26

    Computer Programming II 3

    3

    Access Privileges - ReviewAccess Privileges - Review

    (private, Public, protected)(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 itselfand methods of

    derived class can access it.

    3. Private: methods of class itselfcan access it.

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    4/26

    Computer Programming II 4

    Will you be myWill you be my friend?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 classesfriend methods (member functions)

    Friend-ship is

    A friend function can access both the private and protectedmembers of the class.

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    5/26

    Computer Programming II 5

    FriendsFriends

    function/method/classfunction/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 functionsfriend classes friend methods (member functions)

    - A friend functionis 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 classis a class whose functions can all access private and

    protected data members ofanother class-A friend methodis 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.

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    6/26

    Computer Programming II 6

    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();

    ...

    };

    class Lim{ public:

    Lim(); // constructor

    friend int Yin::Yin_f1();

    ...

    };

    Only the member method Yin_f1 has

    access to private&Protected data ofLim

    The functionsperform_sthg has

    access to private&Protected data of

    Chong

    // can access private&Protected

    //data of Lim

    Example: Friend Function / Method

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    7/26

    Computer Programming II 7

    class Raj

    {

    public:Raj(); // constructor

    int Raj_f1();

    int Raj_f2();

    int Raj_f3();

    };

    class Singh

    {

    public:

    Singh(); // constructor

    friend class Raj;

    ...

    };

    All member methods of

    Raj (f1, f2, f3) have access to

    Private&protected data of Singh

    // can access private&Protected data

    of Singh

    Example: Friend Class

    http://fit.mmu.edu.my/http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    8/26

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    9/26

    Computer Programming II 9

    Friend Method

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    10/26

    Computer Programming II 10

    Friend ClassFriendship is not

    inherited, meaning that

    classes derived from

    YourOtherClass cannotaccess YourClass's

    private members.

    Friendship is not

    transitive, so classes

    that are friends of

    YourOtherClass cannot

    access YourClass's

    private members.

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    11/26

    Computer Programming II 11

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    12/26

    Computer Programming II 12

    Exception HandlingException Handling

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    13/26

    Computer Programming II 13

    Dealing with ErrorsDealing with Errors

    When a program is executed, unexpected situation may occur. Such

    a situation is called anexception

    In other word: Exception is a run-time error caused by someabnormal condition

    Example:

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

    Exception handler

    its a code to deal with these exceptions (run-time errors) when

    they occured

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    14/26

    Computer Programming II 14

    Exception Example

    Division by zero

    float x, y;

    .y = 0.0;

    float result = x/y

    Index out of range

    int array [10];

    for (int i=0; i

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    15/26

    Computer Programming II 15

    Three aspects of dealing with an error:

    1. Identifying that an error has occurred

    2. Handling (resolving) an identified error in order to eithercontinue execution of the program OR gracefullyterminate it.

    3. If the error cannot be resolved at the current level of execution,communicate its existence to the next highest level of execution

    Dealing with Errors

    int main() {int x,y;

    cout > x >> y;

    cout

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    16/26

    Computer Programming II 16

    Introduction to Exceptions

    An exception is an object (int, char, text..etc) that containsinformation on an error and is used to communicate that

    information to a higher level of execution.

    The process of transferring an exception from its currentexecution context to the context above it is knows as

    throwing an exception The point in the function code at which the exception isthrown is referred to as the throw point

    An execution context that receives an exception and

    accesses it is said to catch the exception

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    17/26

    Computer Programming II 17

    The Syntax of Exceptions

    C++ implements exceptions with three keywords

    try, throw and catch

    Try Block

    - The code which mightgenerate a runtime error is writtenwithin the try block.

    Format:

    try {//Code that can generate exceptions

    }

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    18/26

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    19/26

    Computer Programming II 19

    The Syntax of Exceptions

    Catch Block

    - Catches the exception (errors) which is thrown by throwkeyword 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; }

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    20/26

    Computer Programming II 20

    # include

    int main()

    { int value1, value2, result;

    }// end of main

    try

    { cin >> value1; cin >> value2;

    }// end of try

    catch (int a ){ cout

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    21/26

    Computer Programming II 21

    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 parametersin the throw, i.e., OVERLOADING

    The try-catch block

    Th h bl k

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    22/26

    Computer Programming II 22

    The try-catch blocktry {

    // Code that could generate an exception }

    catch () {

    // Code that resolves a type1 exception

    }

    catch () {

    // Code that resolves a type2 exception

    }

    catch () {

    // Code that resolves a typeN exception

    }; // end-of try-catch block

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    23/26

    Computer Programming II 23

    The try-catch block

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    24/26

    Computer Programming II 24

    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

    http://fit.mmu.edu.my/
  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    25/26

    Computer Programming II 25

    Exception Matching

    catch()

    {

    cout

  • 8/14/2019 11602105 C Lecture 12 Friends Exception Handling

    26/26

    Computer Programming II 26

    Exception Matching

    void main ( )

    {

    int a[10], n, I;

    coutn:

    func(n);for(I=0;I>a[I];

    int sum=0;

    for(I=0;I