Slide 1:

Exceptions

  General Description
 

                                              Shawn Stoffer -- CS151 Spring 2001


Slide 2:

Exceptions

    General Description

    They are used to deal with errors.

    If:

    Then:
instead of returning a garbage value, from a function, that can be ignored,
throw an exception, because it must be dealt with.
Shawn Stoffer -- CS151 Spring 2001


Slide 3:

Exceptions
 

    Behavior
 

                                             Shawn Stoffer -- CS151 Spring 2001


Slide 4:

Exceptions

    Behavior (continued)
 



Slide 5:

Exceptions

    Behavior (continued)
 

Shawn Stoffer -- CS151 Spring 2001


Slide 6:

Exceptions

    Syntax
 

Shawn Stoffer -- CS151 Spring 2001


Slide 7:

Exceptions

    Syntax

        throw <exception variable>;

        You can 'throw' anything, a value (1, 4.5, 9.0, etc...), or a variable.

        Whatever you 'throw' is passed with the exception through the functions which called the offending function.  (read: offending function = function that threw the exception).

Shawn Stoffer -- CS151 Spring 2001



Slide 8:

Exceptions

    Syntax
        Example

int getElement(int rows) {
    int i = 5;
    if (rows > MAXROWS) {
        throw 5;
    }
    return table[rows];
}
Shawn Stoffer -- CS151 Spring 2001


Slide 9:

Exceptions

    Syntax
        Example

enum Excepts
{
    OutOfRangeException
};
int getElement(int rows)
{
    Excepts i = OutOfRangeException;
    if (rows > MAXROWS)
    {
        throw i;
    }
    return table[rows];
}
Shawn Stoffer -- CS151 Spring 2001


Slide 10:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001

Slide 11:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 12:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 13:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 14:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 15:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 16:

Exceptions

    Syntax

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 17:

Exceptions

    Syntax  (example)

    try { <statements> } catch (<exception variable>) { <statements> }
 

void afunc(int e) throw(int)
{
    if (e > 5)
    {
        throw -1;
    }
}
 

int i = 9;
try {
    afunc(i);
} catch (int e) {
    if (e == -1) {
        cout << "We encountered the exception for an invalid value!" << endl;
        return;
    }
} catch (...) {
    cout << "An unknown exception was found!!" << endl;
    throw;
}

Shawn Stoffer -- CS151 Spring 2001


Slide 18:

Exceptions

    Syntax (example)

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 19:

Exceptions

    Syntax (example)

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 20:

Exceptions

    Syntax (example)

    try { <statements> } catch (<exception variable>) { <statements> }
 

 
Shawn Stoffer -- CS151 Spring 2001


Slide 21:

Exceptions

    Syntax (example)

    try { <statements> } catch (<exception variable>) { <statements> }

 
Shawn Stoffer -- CS151 Spring 2001