Exceptions

General Description

  • A way of dealing with errors


  • Calling functions must deal with
    • Caller cannot simply ignore them
Shawn Stoffer -- CS151 Spring 2001


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


Exceptions

Behavior






Shawn Stoffer -- CS151 Spring 2001


Exceptions

Behavior (continued)

Shawn Stoffer -- CS151 Spring 2001


Exceptions

Behavior (continued)
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax
Shawn Stoffer -- CS151 Spring 2001


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


Exceptions

Syntax

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


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


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


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


Exceptions

Syntax (example)

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax (example)

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax (example)

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001


Exceptions

Syntax (example)

try { <statements> } catch (<exception variable>) { <statements> }
Shawn Stoffer -- CS151 Spring 2001