Easy C++


C++ Tutorial - Lesson 34: Exceptions


by John Kopp

Support this site at no cost to you

Welcome to EasyCPlusPlus.com's free tutorial on C++ programming. This lesson covers exceptions. Up to this point in the tutorial, we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions. An error typically refers to a problem that exists in a program when it is written and compiled. It could be a logic error that will result in incorrect results. In this case, the code is correct but the algorithm has an error. This type of error will only be found during program testing or during design reviews of the program. Another type of error is a syntax error. Typically, the compiler finds errors of this type and they are corrected during the coding of the program. Exceptions are errors or anomalies that occur during the execution of a program. They can be the result of unavailability of system resources, such as memory, file space, channels or from data dependent conditions such as a divide by zero, or numerical overflow. Exceptions tend to be rare events but are predicable.

Given that exceptions are somewhat predicable, how should our programs handle them? Broadly, there are three types of responses we can take.

  • Not handle the exception. Allow the program to die or core dump.
  • Issue a warning. Print out some type of error message, probably at the spot in the code where the exception occurred and then exit.
  • Handle the exception gracefully and continue executing.
Certainly, the first way, doing nothing is not acceptable if you want to remain employed or pass your courses. The second way is a little better. Information about the exception is written out, but this is still not ideal. Most real world programs need to be more robust than this. Exceptions need to be handled and corrected. Execution must continue. Your mission to reach Mars can't fail due to a divide by zero.

Since we must handle exceptions, what features would assist us? Should the same section of code be raising the exception and handling the exception? Suppose an exception occurs in allocating memory. Should the function or method that attempted the allocation be the one to handle it? Can it? Probably some other, higher level, section of code will have the information necessary to decide how to handle the exception. Maybe different programs using the same classes and methods will handle exceptions differently. This points to a separation of the creation of an exception and its handling. The method in which an exception occurs could just alert its caller. This allows code that raises exceptions to be developed separately from code that handles them. If we pass exceptions up to calling routines, it is necessary to have a way to bundle information and for the exception to have some methods to assist in its handling.

The C++ exception mechanism handles both these features.

  • Exceptions may be raised and handled in different sections of code.
  • Any object, including class objects may be passes back to the handler of an exception. These objects can contain data and methods to assist in handling the exception.

Previous Page       Next Page