Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

download Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

of 26

Transcript of Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    1/26

    Object-Oriented Programming

    (OOP)Lecture No. 44

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    2/26

    Exampleclass DivideByZero {

    public:

    DivideByZero() {

    }

    };int Quotient(int a, int b){

    if(b == 0){

    throw DivideByZero();

    }return a / b;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    3/26

    main Function

    int main() {

    try{

    quot = Quotient(a,b);

    }

    catch(DivideByZero) {

    }return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    4/26

    Stack Unwinding

    The flow control of throw is referred toas stack unwinding

    Stack unwinding is more complex

    than return statement Return can be used to transfer the

    control to the calling function only

    Stack unwinding can transfer thecontrol to any function in nestedfunction calls

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    5/26

    Stack Unwinding

    All the local objects of a executingblock are destroyed when an

    exception is thrown

    Dynamically allocated memory is notdestroyed automatically

    If no catch handler catches the

    exception the function terminate is

    called, which by default calls function

    abort

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    6/26

    Examplevoid function1() { ...

    throw Exception();

    }

    void function2() {

    function1();}

    int main() {

    try{

    function2();} catch( Exception ) { }

    return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    7/26

    Function-Call Stack

    function2()

    main()

    function1()

    function2()

    main()

    main()

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    8/26

    Stack Unwinding

    The stack unwinding is also

    performed if we have nested try

    blocks

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    9/26

    Exampleint main( ) {

    try {

    try {

    throw 1;

    }catch( float ) { }

    }

    catch( int ) { }return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    10/26

    Stack Unwinding

    Firstly the catch handler with floatparameter is tried

    This catch handler will not be

    executed as its parameter is of

    different typeno coercion

    Secondly the catch handler withint parameter is tried and

    executed

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    11/26

    Catch Handler

    We can modify this to use theobject to carry information about

    the cause of error

    The object thrown is copied to the

    object given in the handler

    Use the reference in the catchhandler to avoid problem caused

    by shallow copy

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    12/26

    Exampleclass DivideByZero {

    int numerator;

    public:

    DivideByZero(int i) {

    numerator = i;}

    void Print() const{

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    13/26

    Example

    int Quotient(int a, int b) {if(b == 0){

    throw DivideByZero(a);}

    return a / b;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    14/26

    Body of mainFunctionfor ( int i = 0; i < 10; i++ ) {

    try {

    GetNumbers(a, b);

    quot = Quotient(a, b); ...

    } catch(DivideByZero & obj) {obj.Print();

    i--;

    }}

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    15/26

    OutputEnter two integers

    10

    10

    Quotient of 10 and 10 is 1

    Enter two integers10

    0

    10 was divided by zero...

    // there will be sum of exactly ten quotients

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    16/26

    Catch Handler

    The object thrown as exception is

    destroyed when the execution of

    the catch handler completes

    A idi C h

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    17/26

    Avoiding too many Catch

    Handlers

    There are two ways to catch more

    then one object in a single catch

    handler

    Use inheritance

    Catch every exception

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    18/26

    Inheritance of Exceptions

    MathError

    DivideByZero

    IntergerOutOfRange

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    19/26

    Grouping Exceptionstry{

    ...}

    catch(DivideByZero){

    ...

    }

    catch(IntergerOutOfRange){

    ...

    }catch (InputStreamError){

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    20/26

    ExampleWith Inheritance

    try{...

    }catch (MathError){

    }

    catch (InputStreamError){

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    21/26

    Catch Every Exception

    C++ provides a special syntaxthat allows to catch every objectthrown

    catch ( ... )

    {

    //...

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    22/26

    Re-Throw

    A function can catch an exceptionand perform partial handling

    Re-throw is a mechanism of throw

    the exception again after partial

    handling

    throw;/*without any expression*/

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    23/26

    Exampleint main ( ) {

    try {

    Function();

    }catch(Exception&) {

    ...

    }return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    24/26

    Examplevoid Function( ) {

    try {

    /*Code that might throw

    an Exception*/

    } catch(Exception&){if( can_handle_completely ) {

    // handle exception

    } else {// partially handle exception

    throw;//re-throw exception

    } } }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    25/26

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 44

    26/26

    Exampletry{

    ...

    }

    catch (...) { ...

    }catch ( MathError ) { ...

    }

    catch ( DivideByZero ) { ...}

    // last two handler can never be invoked