Lecture12-Friends Eception Handling

Embed Size (px)

Citation preview

  • 7/21/2019 Lecture12-Friends Eception Handling

    1/27

  • 7/21/2019 Lecture12-Friends Eception Handling

    2/27

    Computer Programming II 2

    OutlinesOutlines

    FriendshipFriendship

    Friend Function Friend Class

    Exception HandlingException Handling Exception

    Exception Handler

    try, throw and catch block

    Exception Propagation

    Multiple catch Blocks

    Exception Matching

    Advantages o Exception Handling

    http://fit.mmu.edu.my/http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    3/27

    Computer Programming II 3

    FriendshipFriendship

    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, eventhough the function itself is not a memberof the

    class.

    A friend class is a class that can access privateand protected members of another class

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    4/27

    Computer Programming II 4

    FriendshipFriendship

    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.hen A ma!es B a friend, B does not

    automatically ma!e A a friend

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    5/27

    Computer Programming II 5

    // Problem

    class Human {

    string secret;

    public:

    Human (string secret)

    : secret(secret) {}

    };

    voidtellSecret (Human h){

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    6/27

    Computer Programming II 6

    // riend &unction solution

    class Human {

    string secret;

    &riendvoidtellSecret (Human);

    public:

    Human (string secret)

    : secret(secret) {}

    };

    voidtellSecret (Human h){ cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    7/27

    Computer Programming II 7

    // Problem

    class Human{

    string secret;

    public:

    Human (string secret)

    : secret(secret) {}

    };

    class Parrot{public:

    void tellSecret(Human h) {

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    8/27

    Computer Programming II 8

    // riend class solution

    class Human{ &riend class Parrot;

    string secret;

    public:

    Human (string secret)

    : secret(secret) {}

    };class Parrot{

    public:

    void tellSecret (Human h) {

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    9/27

    Computer Programming II 9

    Exception HandlingException Handling

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    10/27

    Computer Programming II 10

    "#ception"#ception

    When a program is executed unexpected situation ma!

    occur. "uch a situation is called an exception#n other $ord: Exceptionis a runtime error caused b!

    some abnormal conditions

    Example:

    division b! %erofailure of ne*operator to obtain a re&uested amount of

    memor!

    Exception handleris code that handles the exception'runtime error( $hen it occurs

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    11/27

    Computer Programming II 11

    Exception Example: )ivision *! +ero

    double divide (double +, double -) {

    return + / -; // divide b- i& -

    }

    int main() {

    double +, -;

    cin + -;

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    12/27

    Computer Programming II 12

    Exception Example: )ivision *! +ero

    double divide (double +, double -) {

    return + / -; // divide b- i& -

    }

    int main() {

    double +, -;

    cin + -;

    i&(- ) cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    13/27

    Computer Programming II 13

    C,, implements exception handling using tr- thro*

    and catchbloc

    tr- bloc:-rite the code that might generate runtime error within

    the tr-bloc!.

    Format: tr- {

    // 0ode that ma- generate

    // e+ceptions}

    Exception andling

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    14/27

    Computer Programming II 14

    thro*statement:-)se !eyword thro*in tr-bloc! to signal that abnormal

    condition or error has occurred.-&f the thro*statement is e#ecuted, the codes in the try

    bloc! that appear after the thro*statement won*t be

    e#ecuted.

    Format: tr- {

    ...

    thro*

  • 7/21/2019 Lecture12-Friends Eception Handling

    15/27

    Computer Programming II 15

    catchbloc:-rite the code that catches the thrown e#ception in

    catchbloc!. +his is the exception handler.-)nhandled)ncaught thrown e#ception will terminate the

    program.

    Format: tr- {

    ...

    thro*

  • 7/21/2019 Lecture12-Friends Eception Handling

    16/27

    Computer Programming II 16

    double divide (double +, double -) {

    i& (- )

    thro* -; return + / -;

    }

    int main() {

    double +, -;

    cin + -;

    tr- { double result divide (+, -);

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    17/27

    Computer Programming II 17

    double divide (double +, double -) {

    i& (- )

    thro* -; return + / -;

    }

    int main() {

    double +, -;

    cin + -;

    tr- { double result divide (+, -);

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    18/27

    Computer Programming II 18

    double divide (double +, double -) {

    i& (- )

    thro* -; return + / -;

    }

    int main() {

    double +, -;

    cin + -;

    tr- { double result divide (+, -);

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    19/27

    Computer Programming II 19

    int main() {

    double +, -;

    cin + -;

    tr- {

    i& (- )

    thro* -; double result + / -;

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    20/27

    Computer Programming II 20

    Exception 4ropagation

    #f the function7method containing the thro*statement

    does not catch the exception the exception $ill be

    propagated up to the callerof the function until itreaches a tr-bloc or themain function.

    #n the former case the tr-/catchbloc of the caller

    handles the exception if the exception t!pe matchesone of the catchbloc. Other$ise the exception $ill be

    propagated up again.

    #f the exception has reached themainfunction and is

    not handled the program $ill be terminated.

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    21/27

    Computer Programming II 21

    Example: Exception 4ropagation

    double divide% (double +, double -) {

    i& (- ) thro* -;

    return + / -;

    }

    double divide4 (double +, double -) {

    return divide% (+, -);

    }

    double divide (double +, double -) {

    return divide4 (+, -);

    }

    int main() {

    ...

    tr- {

    double result divide (+, -);

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    22/27

    Computer Programming II 22

    Some times, we might have many different e#ceptions

    for a small bloc! of code

    . e should write as many catchbloc!s.

    . +his also means that we should have as

    many thro*statements.

    /. B)+'usually0, only one tr-bloc!.

    But, which catchbloc! will be instigatedinvo!ed%1epend on the type of parameter

    8ultiple catch*locs

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    23/27

    Computer Programming II 23

    tr-{

    // 0ode that could generate an e+ception

    }catch(

  • 7/21/2019 Lecture12-Friends Eception Handling

    24/27

    Computer Programming II 24

    int main () {

    &unc (4);

    &unc (%); &unc ();

    return ;

    }

    "utput:

    0atch an int argument

    0atch a char argument

    n is not 4, % or

    void &unc (int n) {

    tr- {

    i& (n 4) thro* 44; // int i& (n %) thro* a; // char

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    25/27

    Computer Programming II 25

    Exception 8atching

    +o catch every possible e#ception type, use ellipsis 393

    4imitationsofcatch (...)5ou can*t tell what typeof e#ception occurred(o argument to reference

    Should always be placed as the last catch

    tr- {

    }

    catch (...) { // catches all e+ceptions

    }

    http://fit.mmu.edu.my/
  • 7/21/2019 Lecture12-Friends Eception Handling

    26/27

    Computer Programming II 26

    Exception 8atchingint main () {

    &unc (4);

    &unc (%); &unc ();

    &unc ();

    return ;

    }

    "utput:

    0atch an int argument

    0atch a char argument

    5ot int nor char

    n is not 4, % or

    void &unc (int n) {

    tr- {

    i& (n 4) thro* 44; // int i& (n %) thro* a; // char

    i& (n ) thro* .6; // double

    cout

  • 7/21/2019 Lecture12-Friends Eception Handling

    27/27

    Computer Programming II 27

    dvantages of Exception andling

    ;sing tr- thro* and catchblocs to handleexception offer the follo$ing advantages:

    ou ma! thro$ an exception in afunction7method and handle it some$here else

    http://fit.mmu.edu.my/