Computer Notes - Techniques for Error Handling

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    1/33

    Techniques for Error Handling

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    2/33

    Techniques for Error Handling

    Abnormal termination Graceful termination

    Return the illegal value

    Return error code from a function

    Exception handling

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    3/33

    Example Abnormal Termination

    void GetNumbers( int &a, int &b ) {

    cout > a >> b;

    }

    int Quotient( int a, int b ){return a / b;

    }

    void OutputQuotient( int a, int b, int quo ) {cout

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    4/33

    Example Abnormal Terminationint main(){

    int sum = 0, quot;

    int a, b;for (int i = 0; i < 10; i++){

    GetNumbers(a,b);

    quot = Quotient(a,b);sum += quot;

    OutputQuotient(a,b,quot);

    }cout

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    5/33

    OutputEnter two integers

    1010

    Quotient of 10 and 10 is 1Enter two integers

    100

    Program terminated abnormally

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    6/33

    Graceful Termination

    Program can be designed in such away that instead of abnormal

    termination, that causes the wastageof resources, program performs

    clean up tasks

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    7/33

    Example Graceful Terminationint Quotient (int a, int b ) {if(b == 0){

    cout

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    8/33

    OutputEnter two integers

    1010

    Quotient of 10 and 10 is 1Enter two integers

    100

    Denominator cant be zero

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    9/33

    Error Handling

    The clean-up tasks are of localnature only

    There remains the possibility ofinformation loss

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    10/33

    Example Return Illegal Valueint Quotient(int a, int b){if(b == 0)

    b = 1;OutputQuotient(a, b, a/b);

    return a / b ;

    }int main() {

    int a,b,quot; GetNumbers(a,b);

    quot = Quotient(a,b);

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    11/33

    OutputEnter two integers

    100

    Quotient of 10 and 1 is 10

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    12/33

    Error Handling

    Programmer has avoided the systemcrash but the program is now in an

    inconsistent state

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    13/33

    Example Return Error Code

    bool Quotient ( int a, int b, int & retVal ) {if(b == 0){

    return false;

    }retVal = a / b;

    return true;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    14/33

    Part ofmain Functionfor(int i = 0; i < 10; i++){GetNumbers(a,b);

    while ( ! Quotient(a, b, quot) ) {

    cout

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    15/33

    OutputEnter two integers10

    0

    Denominator cant be zero. Give input again.

    Enter two integers10

    10

    Quotient of 10 and 10 is 1

    ...//there will be exactly ten quotients

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    16/33

    Error Handling

    Programmer sometimes has tochange the design to incorporate

    error handling Programmer has to check the return

    type of the function to know whetheran error has occurred

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    17/33

    Error Handling

    Programmer of calling function canignore the return value

    The result of the function mightcontain illegal value, this may cause

    a system crash later

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    18/33

    Programs Complexity Increases

    The error handling code increases

    the complexity of the code

    Error handling code is mixed with

    program logic

    The code becomes less readable

    Difficult to modify

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    19/33

    Exampleint main() {

    function1();function2();

    function3();

    return 0;

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    20/33

    Exampleint main(){if( function1() ) {

    if( function2() ) {

    if( function3() ) {...

    }

    else cout

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    21/33

    Exception Handling

    Exception handling is a muchelegant solution as compared to

    other error handling mechanisms It enables separation of main logic

    and error handling code

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    22/33

    Exception Handling Process Programmer writes the code that is

    suspected to cause an exception intry block

    Code section that encounters anerrorthrows an object that is used to

    represent exception

    Catch blocks follow try block to catch

    the object thrown

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    23/33

    Syntax - Throw The keyword throw is used to throwan exception

    Any expression can be used torepresent the exception that has

    occurred

    throw X;throw (X);

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    24/33

    Examplesint a;Exception obj;

    throw 1; // literalthrow (a); // variable

    throw obj; // objectthrow Exception();

    // anonymous object

    throw 1+2*9;// mathematical expression

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    25/33

    Throw Primitive data types may be avoided

    as throw expression, as they cancause ambiguity

    Define new classes to represent theexceptions that has occurred

    This way there are less chances of

    ambiguity

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    26/33

    Syntax Try and Catchint main () {try {

    ...}

    catch ( Exception1 ) {

    ...

    }

    catch ( Exception2 obj ) {

    ...

    }return 0;

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    27/33

    Catch Blocks Catch handler must be preceded by

    a try block or an other catch handler Catch handlers are only executed

    when an exception has occurred Catch handlers are differentiated on

    the basis of argument type

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    28/33

    Catch Handler

    The catch blocks are tried in orderthey are written

    They can be seen as switchstatement that do not need break

    keyword

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    29/33

    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 - Techniques for Error Handling

    30/33

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

    try{

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

    OutputQuotient(a,b,quot); sum += quot;

    }

    catch(DivideByZero) {

    i--;

    cout

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    31/33

    OutputEnter two integers

    10

    10

    Quotient of 10 and 10 is 1

    Enter two integers10

    0

    Attempt to divide numerator with zero...

    // there will be sum of exactly ten quotients

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    32/33

    Catch Handler

    The catch handler catches the

    DivideByZero object throughanonymous object

    Program logic and error handling

    code are separated

    We can modify this to use the object

    to carry information about the cause

    of error

    S ti f P L i d

  • 8/3/2019 Computer Notes - Techniques for Error Handling

    33/33

    Separation of Program Logic and

    Error Handlingint main() {try {

    function1();function2();

    function3();

    }catch( ErrorX) { ... }

    catch( ErrorY) { ... }

    catch( ErrorZ) { ... }

    return 0;

    }

    http://ecomputernotes.com