Share via


try, catch, and throw Statements (C++)

C++ exceptions use the try, catch and throw keywords.

A throw expression signals the error or exceptional condition. You can use an object of any type as the operand of a throw expression. This object is typically used to convey information about the error. Typically, you should use the std::exception class or one of the derived classes that are defined in the standard library, or if none of those are appropriate, then derive your own exception class from std::exception.

A try block encloses one or more statements that might throw an exception.

One or more catch blocks immediately follow a try block. Each catch block specifies the type of exception it can handle.

The following syntax shows an example try block and its handlers. Assume that GetNetworkResource() acquires data over a network connect, and that the two exception types are user-defined classes that derive from std::exception Note that the exceptions are passed by reference in the catch statement:

MyData md;
try {
   // code that could throw an exception
   md = GetNetworkResource();
}
catch (networkIOException& e) {
   // code that executes when an exception of type
  // networkIOException is thrown in the try block
//…
// Log error message in the exception object.
   cerr << e.what();
}
catch (myDataFormatException& e) {
   // code that handles another exception type
//…
 cerr << e.what();

}

// The following syntax shows a throw expression:

MyData GetNetworkResource()
{
    //...
    if(IOSuccess == false)
        throw networkIOException("Unable to connect");
    //...
    if(readError)
        throw myDataFormatException("Format error"); 
    // ...
}

Remarks

The code after the try clause is the guarded section of code. The throw expression throws (raises) an exception. The code block after the catch clause is the exception handler, and catches (handles) the exception thrown by the throw expression if the type in the throw and catch expressions are compatible. For a list of rules that govern type-matching in catch blocks, see _____. If the catch statement specifies an ellipsis (...) rather than a type, the catch block handles any type of exception, including C exceptions and system- or application-generated exceptions such as memory protection, divide by zero, and floating-point violations. Because catch blocks are tried in program order, such a handler must be the last handler for its try block. Use catch (…) with caution; typically such a catch block is used to log errors and perform any special cleanup prior to stopping program execution. Do not allow a program to continue unless the catch block knows how to handle the specific exception that is caught.

A throw-expression with no operand re-throws the exception currently being handled. Such an expression should appear only in a catch handler or in a function called from within a catch handler. The re-thrown exception object is the original exception object (not a copy). For example:

try {
   throw CSomeOtherException();
}
catch(...) {  // Catch all exceptions – dangerous!!!
   // Respond (perhaps only partially) to exception
   throw;       // Pass exception to some other handler
}

See Also

Reference

C++ Exception Handling

C++ Keywords

Unhandled C++ Exceptions

__uncaught_exception

Other Resources

Function-try Blocks