Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
65 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Prelude
 What are
 Exceptions?

 Exception Safety
 and Error Handling

 Exception Class
 Final Thoughts

 SmartTest.zip
 Exception.zip
 Printable version
 Discuss this article
 in the forums


What are Exceptions?

I will briefly cover what exceptions are here. I will not delve too deeply here since any C++ book will cover the syntax better than I can. This is meant to be a refresher course. Please consult a book for syntax details.

Exceptions are designed for error handling. There are two kinds of exceptions - hardware or software. The hardware exception occurs when you do a divide by zero, access invalid pointers, and so on. Dealing with hardware exceptions is usually platform dependant, and the C++ standard has no rules on how hardware exceptions are to be dealt with. This is where our discussion will be Windows dependant.

Example

int x = 3, y = 0;

int z = x / y;     // this will cause a hardware divide by 0 exception

Hardware exceptions normally occur because of program bugs. We cannot really do much with them except handling them so we will leave this aside and focus on software exceptions first.

Try/catch/throw

Software exceptions are generated by the programmer using the keywords try, catch and throw. The try keyword is to guard a section of code against an exception; the catch keyword is to handle the exception; the throw keyword is to generate an exception.

Example

// any exception generated here will result in an illegal operation
// because it is not guarded by a try keyword

try
{
  // all the code here is guarded against exceptions

  throw 100;            // generate an exception of type int

  // Program flow will go to the catch (int) handler. If we throw
  // another type of exception that we do not catch, say 'throw "String" ',
  // then we would enter the catch(...) handler

  // code here will not execute because an exception occurred above

}
catch( int )
{
  // handle an exception of type int specifically
}
  catch(...)
{
  // handles all other kinds of exception we do not explicitly catch
}

// code resumes normally here

We can throw any type of exception - an int, a string, a class object. I generate an int exception here for simplicity. Later, we will generate a class object exception to identify the type of error.

The rule is we should catch whatever we throw.

How to try/catch

Therefore, the try/catch section is normally placed in the main section. This will ensure the program will never crash (well almost). When a function encounters an error situation that should end the program, it throws an exception.

Example

// Just some function, note we do not need a try/catch in the function.
void Function()
{
  if ( 1 != 0 )           // fake some error situation
    throw 100;

  // this will never execute
}

int main()
{
  try
  {
    // do some other code

    Function();

    // this will never execute

  }
  catch(...)
  {
    // display some error to the user

    return 0;   // return failure code to system
  }

  return l;         // success
}

Note that there is only one try/catch in the main function. Functions in the program do not need try/catch blocks. You should note also that generating an exception would stop the program flow in the caller. So if a function calls a function that generates an exception, the code below the calling function would not execute.

Example

// A function that calls another function that generates an exception.
// The caller of this function would also have its program flow disrupted.
void First_Function()
{
  Second_Function();   // if an exception is generated in the function, the
                       // exception would be propagated up to the caller
                       // of First_Function, until it reaches a try/catch.

  // code here will not execute
}

This implies that exceptions should only be used to for serious errors and that the program should quit, hence the name Exceptions. Throwing an exception requires a catch handler so if the error is common, then a return code would be more appropriate.

The rule is to only throw exceptions for critical errors and limit your try/catch to the main program function only. Exceptions are errors but errors are not necessarily exceptions.

Rethrow

Sometimes you would like to catch the exception but still propagate the exception upwards. This is known as re-throwing the exception and is done with the throw keyword in a catch handler.

Example

void Function()
{
  try
  {
  }
  catch(...)
  {
    // catch the error

    throw;       // throw the exception upwards
  }

}

This technique is normally used when the function needs to know about a specific type of exception but is not ready to handle the exception yet. I will show you how to use such a method later but you should avoid this technique if possible.

That should be about all you need to know about exception syntax. All of these should be readily available in a good C++ book.





Next : Exception Safety and Error Handling