Return-Path: Delivered-To: apmail-incubator-stdcxx-commits-archive@www.apache.org Received: (qmail 10659 invoked from network); 4 Jun 2007 20:22:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Jun 2007 20:22:52 -0000 Received: (qmail 82213 invoked by uid 500); 4 Jun 2007 20:22:54 -0000 Delivered-To: apmail-incubator-stdcxx-commits-archive@incubator.apache.org Received: (qmail 82200 invoked by uid 500); 4 Jun 2007 20:22:54 -0000 Mailing-List: contact stdcxx-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: stdcxx-dev@incubator.apache.org Delivered-To: mailing list stdcxx-commits@incubator.apache.org Received: (qmail 82161 invoked by uid 99); 4 Jun 2007 20:22:54 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jun 2007 13:22:54 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jun 2007 13:22:49 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 2257E1A981C; Mon, 4 Jun 2007 13:22:29 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r544245 - /incubator/stdcxx/trunk/doc/stdlibug/18-4.html Date: Mon, 04 Jun 2007 20:22:28 -0000 To: stdcxx-commits@incubator.apache.org From: sebor@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070604202229.2257E1A981C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sebor Date: Mon Jun 4 13:22:27 2007 New Revision: 544245 URL: http://svn.apache.org/viewvc?view=rev&rev=544245 Log: 2007-06-04 Martin Sebor * 18-4.html: Updated example program to match the latest code and added possible output. Modified: incubator/stdcxx/trunk/doc/stdlibug/18-4.html Modified: incubator/stdcxx/trunk/doc/stdlibug/18-4.html URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/doc/stdlibug/18-4.html?view=diff&rev=544245&r1=544244&r2=544245 ============================================================================== --- incubator/stdcxx/trunk/doc/stdlibug/18-4.html (original) +++ incubator/stdcxx/trunk/doc/stdlibug/18-4.html Mon Jun 4 13:22:27 2007 @@ -6,54 +6,65 @@ Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

18.4 Example Program: Exceptions


-NOTE -- This program is in the file exceptn.cpp. +NOTE -- This program is in the file tutorial/stdexcept.cpp.

This following example program demonstrates the use of exceptions:

    -#include <stdexcept>
    -#include <string>
    +#include <string>      // for string
    +#include <stdexcept>   // for exception, runtime_error, out_of_range
    +#include <iostream>    // for cout
     
    -static void f() { throw std::runtime_error("a runtime error"); }
     
     int main ()
     {
    -  std::string s;
    -  
    -  // First we'll try to incite then catch an exception 
    -  // from the C++ 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 an 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 std::exception& e)
    -  {
    -    std::cout << "Got an exception: " << e.what() << std::endl;
    -  }
    +    // First we'll incite and catch an exception from the C++ Standard
    +    // library class std::string by attempting to replace a substring
    +    // starting at a position beyond the end of the string object.
    +    try {
    +        std::string ().replace (100, 1, 1, 'c');
    +    }
    +    catch (std::out_of_range &e) {
     
    -  // Now we'll throw our own exception using the function 
    -  // defined above.
    -  try
    -  {
    -    f();
    -  }
    -  catch (const std::exception& e)
    -  {
    -    std::cout << "Got an exception: " << e.what() << std::endl;
    -  }
    - 
    -  return 0;
    +        // Print out the exception string, which in this implementation
    +        // includes the location and the name of the function that threw
    +        // the exception along with the reason for the exception.
    +        std::cout << "Caught an out_of_range exception: "
    +                  << e.what () << '\n';
    +    }
    +    catch (std::exception &e) {
    +
    +        std::cout << "Caught an exception of an unexpected type: "
    +                  << e.what () << '\n';
    +    }
    +    catch (...) {
    +        std::cout << "Caught an unknown exception\n";
    +    }
    +    
    +    // Throw another exception.
    +    try {
    +        throw std::runtime_error ("a runtime error");
    +    }
    +    catch (std::runtime_error &e) {
    +        std::cout << "Caught a runtime_error exception: "
    +                  << e.what () << '\n';
    +    }
    +    catch (std::exception &e) {
    +        std::cout << "Caught an exception of an unexpected type: "
    +                  << e.what () << '\n';
    +    } 
    +    catch (...) {
    +        std::cout << "Caught an unknown exception\n";
    +    }
    +   
    +    return 0;
     }
     
- +

The exact output of the program is specific to the compiler used to compile it and to the location of the library headers but may look something like this:

    +Caught an out_of_range exception: /usr/local/stdcxx/include/string.cc:422: std::string& std::string::replace(size_type, size_type, size_type, char_type): argument value 100 out of range [0, 0)
    +Caught a runtime_error exception: a runtime error
    +