C++ STL iterator, checked iterators program sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the checked iterators to ensure that you do not overwrite the bounds of your container in C++ programming

To show: How to implement the checked iterators in C++ programming to ensure that you do not overwrite the bounds of your container

 

// C++ STL iterator, checked iterators

#include <vector>

#include <iostream>

// for exception, out_of_range, runtime_error

#include <stdexcept>

using namespace std;

 

int main(void)

{

vector<int> vec;

vec.push_back(100);

// initialize outside try block, so can be used outside try block such as in catch

int i = vec[0];

 

try {

i = vec[1];

cout<<"The push_back element is: ";

cout<<i<<endl;

}

catch (exception &e)

{

// cout<<"out of range!"<<e.what()<<endl;

cout << "Caught: " << e.what( ) << endl;

cout << "Type: " << typeid( e ).name( ) << endl;

}

catch (...) {

cout << "Unknown exception: " << endl;

}

return 0;

}

 

Output examples:

 

The push_back element is: 100

Press any key to continue . . .

 

Change the following code: i = vec[0]; in the try block to i = vec[1];

 

Rebuild and re-run the program. The Debug Assertion Failed error message should be expected.

 

 

 

 

Some note on try-catch-throw block:

 

The operand of throw is syntactically similar to the operand of a return statement. The execution order is:

  1. Control reaches the try statement by normal sequential execution. The guarded section within the try block is executed.

  2. If no exception is thrown during execution of the guarded section, the catch clauses that follow the try block are not executed. Execution continues at the statement after the last catch clause following the try block in which the exception was thrown.

  3. If an exception is thrown during execution of the guarded section or in any routine the guarded section calls (either directly or indirectly), an exception object is created from the object created by the throw operand. (This implies that a copy constructor may be involved.) At this point, the compiler looks for a catch clause in a higher execution context that can handle an exception of the type thrown (or a catch handler that can handle any type of exception). The catch handlers are examined in order of their appearance following the try block. If no appropriate handler is found, the next dynamically enclosing try block is examined. This process continues until the outermost enclosing try block is examined.

  4. If a matching handler is still not found, or if an exception occurs while unwinding, but before the handler gets control, the predefined run-time function terminate is called. If an exception occurs after throwing the exception, but before the unwind begins, terminate is called.

  5. If a matching catch handler is found, and it catches by value, its formal parameter is initialized by copying the exception object. If it catches by reference, the parameter is initialized to refer to the exception object. After the formal parameter is initialized, the process of unwinding the stack begins. This involves the destruction of all automatic objects that were constructed (but not yet destructed) between the beginning of the try block associated with the catch handler and the exception's throw site. Destruction occurs in reverse order of construction. The catch handler is executed and the program resumes execution following the last handler (that is, the first statement or construct which is not a catch handler). Control can only enter a catch handler through a thrown exception, never via a goto statement or a case label in a switch statement.

 

 

C and C++ Programming Resources | C & C++ Code Example Index