+ All Categories
Home > Documents > Computer Notes - Stack

Computer Notes - Stack

Date post: 06-Apr-2018
Category:
Upload: ecomputernotes
View: 218 times
Download: 0 times
Share this document with a friend

of 26

Transcript
  • 8/3/2019 Computer Notes - Stack

    1/26

    Stack

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    2/26

    Exampleclass DivideByZero {public:

    DivideByZero() {}

    };

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

    throw DivideByZero();

    }

    return a / b;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    3/26

    main Functionint main() {

    try{ quot = Quotient(a,b);

    }catch(DivideByZero) {

    }

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    4/26

    Stack Unwinding The flow control of throw is referred to as

    stack unwinding

    Stack unwinding is more complex thanreturn statement

    Return can be used to transfer thecontrol to the calling function only

    Stack unwinding can transfer the controlto any function in nested function calls

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    5/26

    Stack Unwinding All the local objects of a executing block

    are destroyed when an exception is

    thrown

    Dynamically allocated memory is not

    destroyed automatically

    If no catch handler catches the exception

    the function terminate is called, which bydefault calls function abort

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    6/26

    Examplevoid function1() { ...throw Exception();

    }void function2() {

    function1();

    }int main() {

    try{

    function2();

    } catch( Exception ) { }

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    7/26

    Function-Call Stack

    function2()

    main()

    function1()

    function2()

    main()main()

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    8/26

    Stack Unwinding

    The stack unwinding is alsoperformed if we have nested try

    blocks

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    9/26

    Exampleint main( ) {try {

    try {throw 1;

    }

    catch( float ) { }}

    catch( int ) { }

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    10/26

    Stack Unwinding Firstly the catch handler with float

    parameter is tried This catch handler will not be

    executed as its parameter is ofdifferent type no coercion

    Secondly the catch handler with intparameter is tried and executed

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    11/26

    Catch Handler We can modify this to use the object

    to carry information about the causeof error

    The object thrown is copied to theobject given in the handler

    Use the reference in the catchhandler to avoid problem caused by

    shallow copyhttp://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    12/26

    Exampleclass DivideByZero {int numerator;

    public:DivideByZero(int i) {

    numerator = i;

    }void Print() const{

    cout

  • 8/3/2019 Computer Notes - Stack

    13/26

    Exampleint Quotient(int a, int b) {

    if(b == 0){throw DivideByZero(a);

    }return a / b;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    14/26

    Body ofmain Functionfor ( int i = 0; i < 10; i++ ) {try {

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

    } catch(DivideByZero & obj) {

    obj.Print();i--;

    }

    }

    cout

  • 8/3/2019 Computer Notes - Stack

    15/26

    OutputEnter two integers10

    10Quotient of 10 and 10 is 1

    Enter two integers

    100

    10 was divided by zero

    ...

    // there will be sum of exactly ten quotients

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    16/26

    Catch Handler

    The object thrown as exception isdestroyed when the execution of the

    catch handler completes

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    17/26

    Avoiding too many Catch Handlers

    There are two ways to catch morethen one object in a single catch

    handlerUse inheritance

    Catch every exception

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    18/26

    Inheritance of Exceptions

    MathError

    DivideByZero

    IntergerOutOfRange

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    19/26

    Grouping Exceptionstry{...

    }catch(DivideByZero){

    ...

    }

    catch(IntergerOutOfRange){

    ...

    }

    catch (InputStreamError){}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    20/26

    ExampleWith Inheritancetry{

    ...}

    catch (MathError){}

    catch (InputStreamError){

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    21/26

    Catch Every Exception C++ provides a special syntax thatallows to catch every object thrown

    catch ( ... )

    {

    //...

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    22/26

    Re-Throw A function can catch an exception

    and perform partial handling Re-throw is a mechanism of throw

    the exception again after partialhandling

    throw; /*without any expression*/

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    23/26

    Exampleint main ( ) {try {

    Function();}

    catch(Exception&) {...

    }

    return 0;}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    24/26

    Examplevoid Function( ) {try {

    /*Code that might throwan Exception*/

    } catch(Exception&){

    if( can_handle_completely ) {// handle exception

    } else {

    // partially handle exception

    throw; //re-throw exception

    } } }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    25/26

    Order of Handlers

    Order of the more then one catchhandlers can cause logical errors

    when using inheritance or catch all

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Stack

    26/26

    Exampletry{...

    }catch (...) { ...

    }

    catch ( MathError ) { ...}

    catch ( DivideByZero ) { ...

    }

    // last two handler can never be invoked

    http://ecomputernotes.com


Recommended