Skip to main content

6.7) Exercise 10


  1. Open the provided program elements.cpp in your text editor. Notice that the program creates a vector containing 5 integers, all with the value 1. It uses [] to index elements of the vector. Two uses of [] are correct and the third attempts to index a non-existent ninth element.
  2. Compile and run the program. What do you see as output? Notice the absence of error messages.
  3. Alter the program so that it uses the at method instead of []. Compile it and run it again. How has the behaviour changed?

The error that you see here is an exception. Exceptions can be caught in order to recover from an error, should that be possible. Uncaught exceptions will terminate a program.