Try

(Programming::C++ term)

See Also: catch, throw, exception

In the C++ programming language, the try keyword can be used to encase a code block that may cause an exception. If an exception is caught while code is being processed within the try block, then a matching catch block is used to handle that exception. If no matching catch block is found, then the C++ Compiler will usually default to a built in generic exception handler.


To demonstrate a simple, self-contained C++ program that makes use of of a try/catch block:
    #include <iostream>
    using std::cout;

    // Empty class to THROW if a divide by zero is caught
    class XDivideByZero{ };

    int main ( int argc, char **argv ) {
        int result;
        int numberOne = 7;
        int numberTwo = 0;

        try {
            // Test to make sure that zero will not be used in division
            if (!numberTwo > 0)
                // THROW the emtpy class as the exception
                throw XDivideByZero();
            // The code should not reach this point!
            result = (numberOne / numberTwo);
            }

        catch (XDivideByZero) {
            // Handle the Divide By Zero Exception
            cout << "Exception caught: Divide By Zero!\n";
            // Exit with error status (1)
            return(1);
            }

        // The code should not reach this point!        
        cout << "No Errors: Result = " << result << std::endl;
        return(0);
        }


Try compiling and running this code. You should get output stating:

Exception caught: Divide By Zero!


Now, try compiling and running the following code (that does not contain a try/catch block):

    #include <iostream>
    using std::cout;

    int main ( int argc, char **argv ) {
        int result;
        int numberOne = 7;
        int numberTwo = 0;

        result = (numberOne / numberTwo);

        cout << "Result = " << result << std::endl;
        return(0);
        }


You will most likely receive some type of system error, maybe even multiple times. The try/catch block eliminates this error and allows the programmer to determine what should be done if such a situation does exist. In the earlier case (the example using the catch/try block), the program exits with an error code ( return(1) ). If you had dynamically allocated data, you would be able to remove it within the catch block. If no catch block was found, and depending upon your compiler, you may have just created a memory leak (unless of course a deconstructor is called to remove the allocated memory. This is beyond the scope of this section, and I encourage you reference such subjects; they will not, however, be discussed here.

try/catch blocks can be nested within each other (just like if statements and loop constructs).

When an exception is thrown in a try block, the program unwinds until it reaches an appropriate catch block. This means that once an exception is thrown, the execution of the program begins back-stepping through the program until it reaches a catch block.


Please view the node catch for more detailed information on what you can catch