Top of document
©Copyright 1999 Rogue Wave Software

Example Program

Obtaining the Sample Program.

This following example program demonstrates the use of exceptions.

#include <stdexcept>
 #include <string>
 
 static void f() { throw runtime_error("a runtime error"); }
 
 int main ()
 {
   string s;
   
   // First we'll try to incite then catch an exception from
   // the standard library string class.
   // We'll try to replace at a position that is non-existent.
   //
   // By wrapping the body of main in a try-catch block we can be
   // assured that we'll catch all exceptions in the exception
   // hierarchy. You can simply catch exception as is done below,
   // or you can catch each of the exceptions in which you have an 
   // interest.
   try
   {
     s.replace(100,1,1,'c');
   }
   catch (const exception& e)
   {
     cout << "Got an exception: " << e.what() << endl;
   }
 
   // Now we'll throw our own exception using the function 
   // defined above.
   try
   {
     f();
   }
   catch (const exception& e)
   {
     cout << "Got an exception: " << e.what() << endl;
   }
  
  return 0;
 }

Top of document