www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 835 (continued)]

Summary

  • An exception is an indication of a problem that occurs during a program's execution.

  • Exception handling enables programmers to create programs that can resolve problems that occur at execution timeoften allowing programs to continue executing as if no problems had been encountered. More severe problems may require a program to notify the user of the problem before terminating in a controlled manner.

  • Exception handling enables the programmer to remove error-handling code from the "main line" of the program's execution, which improves program clarity and enhances modifiability.

  • C++ uses the termination model of exception handling.

  • A try block consists of keyword try followed by braces ({}) that define a block of code in which exceptions might occur. The try block encloses statements that might cause exceptions and statements that should not execute if exceptions occur.

  • At least one catch handler must immediately follow a try block. Each catch handler specifies an exception parameter that represents the type of exception the catch handler can process.

  • If an exception parameter includes an optional parameter name, the catch handler can use that parameter name to interact with a caught exception object.

  • The point in the program at which an exception occurs is called the throw point.

  • If an exception occurs in a try block, the try block expires and program control transfers to the first catch in which the exception parameter's type matches that of the thrown exception.

  • When a try block terminates, local variables defined in the block go out of scope.

  • When a try block terminates due to an exception, the program searches for the first catch handler that can process the type of exception that occurred. The program locates the matching catch by comparing the thrown exception's type to each catch's exception-parameter type until the program finds a match. A match occurs if the types are identical or if the thrown exception's type is a derived class of the exception-parameter type. When a match occurs, the code contained within the matching catch handler executes.

  • When a catch handler finishes processing, the catch parameter and local variables defined within the catch handler go out of scope. Any remaining catch handlers that correspond to the TRy block are ignored, and execution resumes at the first line of code after the TRy ... catch sequence.

  • If no exceptions occur in a TRy block, the program ignores the catch handler(s) for that block. Program execution resumes with the next statement after the TRy ... catch sequence.

  • If an exception that occurs in a TRy block has no matching catch handler, or if an exception occurs in a statement that is not in a TRy block, the function that contains the statement terminates immediately, and the program attempts to locate an enclosing try block in the calling function. This process is called stack unwinding.

  • Exception handling is for synchronous errors, which occur when a statement executes.


  • [Page 836]
  • Exception handling is not designed to process errors associated with asynchronous events, which occur in parallel with, and independent of, the program's flow of control.

  • To throw an exception, use keyword throw followed by an operand that represents the type of exception to throw. Normally, a throw statement specifies one operand.

  • The operand of a tHRow can be of any type.

  • The exception handler can defer the exception handling (or perhaps a portion of it) to another exception handler. In either case, the handler achieves this by rethrowing the exception.

  • Common examples of exceptions are out-of-range array subscripts, arithmetic overflow, division by zero, invalid function parameters and unsuccessful memory allocations.

  • Class exception is the standard C++ base class for exceptions. Class exception provides virtual function what that returns an appropriate error message and can be overridden in derived classes.

  • An optional exception specification enumerates a list of exceptions that a function can throw. A function can throw only exceptions of the types indicated by the exception specification or exceptions of any type derived from these types. If the function throws an exception that does not belong to a specified type, function unexpected is called and the program normally terminates.

  • A function with no exception specification can throw any exception. The empty exception specification throw() indicates that a function does not throw exceptions. If a function with an empty exception specification attempts to throw an exception, function unexpected is invoked.

  • Function unexpected calls the function registered with function set_unexpected. If no function has been registered in this manner, function terminate is called by default.

  • Function set_terminate can specify the function to invoke when terminate is called. Otherwise, terminate calls abort, which terminates the program without calling the destructors of objects that are declared static and auto.

  • Functions set_terminate and set_unexpected each return a pointer to the last function called by terminate and unexpected, respectively (0, the first time each is called). This enables the programmer to save the function pointer so it can be restored later.

  • Functions set_terminate and set_unexpected take as arguments pointers to functions with void return types and no arguments.

  • If a programmer-defined termination function does not exit a program, function abort will be called after the programmer-defined termination function completes execution.

  • Unwinding the function call stack means that the function in which the exception was not caught terminates, all local variables in that function are destroyed and control returns to the statement that originally invoked that function.

  • Class runtime_error (defined in header <stdexcept>) is the C++ standard base class for representing runtime errors.

  • Exceptions thrown by a constructor cause destructors to be called for any objects built as part of the object being constructed before the exception is thrown.

  • Destructors are called for every automatic object constructed in a try block before an exception is thrown.

  • Stack unwinding completes before an exception handler begins executing.

  • If a destructor invoked as a result of stack unwinding throws an exception, terminate is called.

  • If an object has member objects, and if an exception is thrown before the outer object is fully constructed, then destructors will be executed for the member objects that have been constructed before the exception occurs.


  • [Page 837]
  • If an array of objects has been partially constructed when an exception occurs, only the destructors for the constructed array element objects will be called.

  • When an exception is thrown from the constructor for an object that is created in a new expression, the dynamically allocated memory for that object is released.

  • If a catch handler catches a pointer or reference to an exception object of a base-class type, it also can catch a pointer or reference to all objects of classes derived publicly from that base classthis allows for polymorphic processing of related errors.

  • The C++ standard document specifies that, when operator new fails, it throws a bad_alloc exception (defined in header file <new>).

  • Function set_new_handler takes as its argument a pointer to a function that takes no arguments and returns void. This pointer points to the function that will be called if new fails.

  • Once set_new_handler registers a new handler in the program, operator new does not throw bad_alloc on failure; rather, it defers the error handling to the new-handler function.

  • If new allocates memory successfully, it returns a pointer to that memory.

  • If an exception occurs after successful memory allocation but before the delete statement executes, a memory leak could occur.

  • The C++ Standard Library provides class template auto_ptr to deal with memory leaks.

  • An object of class auto_ptr maintains a pointer to dynamically allocated memory. An auto_ptr object's destructor performs a delete operation on the auto_ptr's pointer data member.

  • Class template auto_ptr provides overloaded operators * and -> so that an auto_ptr object can be used just as a regular pointer variable is. An auto_ptr also transfers ownership of the dynamic memory it manages via its copy constructor and overloaded assignment operator.

  • The C++ Standard Library includes a hierarchy of exception classes. This hierarchy is headed by base-class exception.

  • Immediate derived classes of base class exception include runtime_error and logic_error (both defined in header <stdexcept>), each of which has several derived classes.

  • Several operators throw standard exceptionsoperator new throws bad_alloc, operator dynamic_cast tHRows bad_cast and operator typeid throws bad_typeid.

  • Including bad_exception in the throw list of a function means that, if an unexpected exception occurs, function unexpected can throw bad_exception rather than terminating the program's execution or calling another function specified by set_unexpected.


Previous Page
Next Page