Throw

(Programming::C++ term)

See Also: try, catch, exception

Note: Please read try and catch before reading this!

In the C++ programming language, the throw keyword can be used to raise an exception. If you are not already familiar with the concepts of a try and catch block, I encourage you to refer to those nodes first before continuing here. With that settled, let us evaluate the throw keyword in better detail:

The throw keyword is usually used in try blocks to throw an exception. The thrown exception can be anything from an invalid memory access function call to a user-defined safety condition.

Some exceptions that come with the ANSI/ISO Standard C++ Library include:

  • bad_alloc - This is thrown when an attempt to use the new operator fails. This commonly means that an error occured while trying to allocate memory that was unavailable.
  • bad_typeid - This is thrown when an error occurs during a typeid statement. This can mean that the program was unable to "type" the variable.
  • bad_cast - This is thrown when an error occurs during a cast from one type to another. This commonly happens in invalid dynamic_cast statements, but can also be thrown during other casts.
  • out_of_range - This is thrown when a functions argument was not within a specified range. This is not a common exception. This can happen when you attempt to mix types via casts.
  • invalid_argument - This is thrown when an invalid argument is given to a function. This is rare and can be avoided by specifying exactly what arguments you want a function to take, rather than being ambiguous.
  • length_error - This is thrown when an object you attempted to create was too large. If you feel that a certain class or object is rather big, you should use the new operator to dynamically allocate space for it.
  • overflow_error - This is thrown when an overflow occurs in arithmetic expressions and statements. Proper logic can prevent this.
  • underflow_error - This is thrown when an underflow occurs in arithmetic expressions and statemts. Proper logic can prevent this.
  • range_error - This is thrown when a specified range of an object is not valid. This commonly occurs in trying to assign values to objects that are too small to contain them. If an int object assignment is throwing this exception, try using a larger object (i.e. a double long).

Many more exceptions are defined that you are allowed to throw, but these are the most common. Refer to your compiler's documentation for more exceptions available to you.

Most of the exceptions listed above are thrown by C++ Standard Library classes and function calls. You will probably never need to throw most of them, but you may need to catch them.

You do not always have to throw a specific object or class. You can throw any value you wish and catch it with a catch block. An example of using a primitive int object:


    #include <iostream>
    using std::cout;

    int main(int argc, char **argv){

        // The try block needed to throw the variable
        try {
            cout << "I am going to throw the number 13 at you!\n";
            // Throw a value. In this case, it's the value 13
            throw (13);
            }

        // Now catch the value
        catch (int errorCode) {
            cout << "I caught It! The value is: " << errorCode;
            // Exit with an error status ( return (1) )
            return(1);
            }

        // The execution should not reach this point!
        cout << "What number? I didn't catch any damn number!\n";
        // Return successfully ( return(0) )
        return(0);
        }


Your output from this program should read:

I am going to throw the number 13 at you!
I caught It! The value is: 13

Two header files you should be aware of are stdexcept and exception. The exception header defines the base class exception from which all other exceptions are derived (see inheritance). These two header files declare the exceptions listed above (as well as others) and some functions. The functions are: