Terminology Question

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, const char * argv[])
{

    // insert code here...
    string lang = "C++";
    int num = 1000000000;   // One billion.
    // Try-catch block goes here.
    cout << "Program continues..." << endl;
    try { lang.replace(100, 1, "C");}
    catch(out_of_range &e)
    {
        cerr << "Range Exception: " << e.what() << endl;
        cerr << "Exception Type: " << typeid(e).name();
        cerr << endl << "Program terminated." << endl;
        return -1;
    }
    cin.get();
    return 0;
}


I was wondering what "Range Exception" and "Exception Type" is.

Thanks.
Last edited on
closed account (zb0S216C)
A range exception is an exception thrown by the STL's containers. This exception is thrown when an iterator, for example, exceeds the upper-bound index of some container. For instance, let's say you had a "std::vector" with 6 elements -- the upper-bound index would be 5, and attempting to access the 6th element will cause the container to throw a "std::out_of_range" exception to indicate the error.

An "exception type" is the type of exception that was raised. In C++, exceptions can be of any type, but in the standard library, exceptions come in the form of "struct"s which derive from "std::exception". Therefore, the type of exception is the type used to represent the exception as well as the name if it has one. Here are some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Exception Type A:
throw( "A description of the exception" ); // throw( char const * )

// Exception Type B:
struct Unique_Exception
    : std::exception
  {
    char const *what( ) const
      {
        return( "A description of the exception" );
      }
  };

throw( Unique_Exception( ) );

// Exception Type C:
enum Exception_Code
  {
    Range_Error,
    Access_Violation
  };

throw( Range_Error );

Get the idea?

Wazzak
Last edited on
Topic archived. No new replies allowed.