error1.cpp

URL: http://147.64.242.52/~bennett/class/cmsc2040/fall2026/notes/except/code/error1.cpp
 
#include <iostream>
#include <string>

using namespace std;

int main() {
   string phrase{"Hello World!"};

   for(size_t i = 0; i <= phrase.size(); ++i) {
      try {
         cout << i << " '"  << phrase.at(i) << "'" << endl;
      } catch (...) {
         cout << endl << endl;
         cout << "In the exception handler" << endl;
         cout << i << " must be bad! " << endl;
         cout << endl;
      }
   }

   cout << "This line will always print out" << endl;

   return 0;
}