Apache C++ Standard Library User's Guide

## 18.4 Example Program: Exceptions

> NOTE -- This program is in the file `tutorial/stdexcept.cpp`.

This following example program demonstrates the use of exceptions:

    #include <string>      // for string
    #include <stdexcept>   // for exception, runtime_error, out_of_range
    #include <iostream>    // for cout

    int main ()
    {
        // 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) {

            // 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