throw is part of C++’s exception-handling subsystem. Exception handling is built upon three keywords: try, catch, and throw. In the most general terms, program statements that you want to monitor for exceptions are contained in a try block. If an exception (i.e., an error) occurs within the try block, it is thrown (using throw). The exception is caught, using catch, and processed. The following discussion elaborates upon this general description.

As stated, any statement that throws an exception must have been executed from within a try block. (Functions called from within a try block may also throw an exception.) Any exception must be caught by a catch statement that immediately follows the try statement that throws the exception. The general form of try and catch are shown here:

 try {     // try block } catch (type1 arg) {     // catch block } catch (type2 arg) {     // catch block } catch (type3 arg) {     // catch block } // ... catch (typeN arg) {     // catch block }

The try block must contain that portion of your program that you want to monitor for errors. This can be as short as a few statements within one function or as all-encompassing as enclosing the main( ) function code within a try block (which effectively causes the entire program to be monitored).

When an exception is thrown, it is caught by its corresponding catch statement, which processes the exception. There can be more than one catch statement associated with a try. The catch statement to execute is determined by the type of the exception. That is, if the data type specified by a catch matches that of the exception, then that catch statement is executed (and all others are bypassed). When an exception is caught, arg will receive its value. Any type of data can be caught, including classes that you create. If no exception is thrown (that is, no error occurs within the try block), then no catch statement is executed.

The general form of the throw statement is shown here:

throw exception;

throw must be executed either from within the try block proper, or from any function called (directly or indirectly) from within the try block. exception is the value thrown.

If you throw an exception for which there is no applicable catch statement, an abnormal program termination may occur. Throwing an unhandled exception causes the terminate( ) function to be invoked. By default, terminate( ) calls abort( ) to stop your program. However, you may specify your own handlers if you like, using set_terminate( ).

Here is a simple example that shows the way C++ exception handling operates:

// A simple exception handling example. #include <iostream> using namespace std; int main() {   cout << "Start\n";   try { // start a try block     cout << "Inside try block\n";     throw 100; // throw an error     cout << "This will not execute";   }   catch (int i) { // catch an error     cout << "Caught an exception -- value is: ";     cout << i << "\n";   }   cout << "End";   return 0; }

This program displays the following output:

Start Inside try block Caught an exception -- value is: 100 End

As you can see, there is a try block containing three statements and a catch(int i) statement that processes an integer exception. Within the try block, only two of the three statements will execute: the first cout statement and the throw. Once an exception has been thrown, control passes to the catch expression and the try block is terminated. That is, catch is not called. Rather, program execution is transferred to it. (The program’s stack is automatically reset as needed to accomplish this.) Thus, the cout statement following the throw will never execute.

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539 © 2008-2017.
If you may any questions please contact us: