Namespaces
Variants
Views
Actions

try-catch statement

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
try-catch block
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

Used to attempt the execution of a compound-statement, while catching and handling exceptions that may have been thrown as a result of this attempt.

Contents

[edit] Syntax

try { statements } catch ( exception-decl ) { statements } (1)
try { statements } catch ( exception-decl-1 ) { statements } catch ( exception-decl-2 ) { statements } (2)
try { statements } catch ( exception-decl ) { statements throw; } (3)
try { statements } catch ( ... ) { statements } (4)
try : ctor-init-list { statements } catch ( exception-decl ) { statements } (5)

[edit] Explanation

See throw exceptions for more information about throw exceptions

The basic syntax (1) shows the basic components of a try-catch block. First, the keyword try initiates a try-block in which statements can be executed. Note that the try-block, from the opening curly-brace to its closing, follows the same scoping rules as any other scope (e.g. variables declared within the try-block are not available outside of it, including from the catch-block that follows). Then, the catch statement acts as a specifier of the type of exception to be caught if thrown from within the try-block. The exception-decl has the same syntax as the parameter declaration within a single-parameter function declaration (except that the type cannot by void, an incomplete type, or an rvalue-reference (since C++11)). Finally, the compound-statement { statements which follow the catch-statement is called the exception-handler and contains statements to be executed in response to the exception that was caught. Typical exception-handling code includes logging the error, employing an alternative method to what was attempted in the try-block, or re-packaging the exception into another thrown exception with additional information.

In syntax (2), the illustration here is that a try-catch block is not limited to a single catch-block. Since different types of exceptions can be thrown from within the try-block, one can specify as many catch-blocks as necessary to handle all exceptions one wishes to handle. Note, however, that the order of appearance of the catch-statements is important, a thrown exception will be handled by the first catch-block, by order of appearance, whose exception-decl is a valid match (and implicit conversions apply as well). In other words, it is not the best match that is chosen (as in function overloading rules), but the first match. If the thrown exception matches none of the catch-statements, then the exception is carried back until another enclosing try-block is reached or until the program is terminated due to an unhandled exception.

In syntax (3), the only addition is the throw; statement within the catch-block. The statement has the effect of re-throwing the same exception object which was caught by the catch-block. This is the only context in which an empty throw-statement is valid, and it's specific meaning is to re-throw the exception that was caught, and declared as exception-decl.

Syntax (4) is a so-called catch-all block. The ellipsis operator ... (literally, three dots) can be used in-place of the exception-decl to specify that any and all types of exceptions should be caught by the catch-block. This type of catch-all block is not useful for getting any information on the type of exception that was thrown, and since most exception objects are of classes derived from std::exception, it is generally more useful to catch the exception as catch( std::exception& e ). However, the main purpose of a catch-all block is to ensure that no uncaught exceptions are leaked from a function, which is especially useful for special functions from which leaking exceptions can be dangerous, most notably, a destructor or a dynamically-linked external function.

Syntax (5) is called a function-try-block and can be used to enclose an entire function body inside a try-block (with catch-blocks following it). This is especially useful to catch exceptions which could be thrown during the execution of a constructor's initialization list (construction of sub-objects of a class), in fact, it is the only way to do so. This function-try-block syntax is seldom used in any other context because it has no advantage as compared to a traditional try-catch block, and the resulting syntax is generally unappealing (and unfamiliar to most).

[edit] Keywords

try, catch, throw

[edit] Example

The following example demonstrates several usage cases of the try-catch block

#include <iostream>
#include <vector>
 
int main()
{
    try {
        std::cout << "Throwing an integer exception...\n";
        throw int(42);
    } catch( int i ) {
        std::cout << " the integer exception was caught, with value: " << i << '\n';
    }
 
    try {
        std::cout << "Creating a vector of size 5... \n";
        std::vector<int> v(5);
        std::cout << "Accessing the 11th element of the vector...\n";
        v.at(10);  // the at() function will check the range.
    } catch( std::exception& e) {
        std::cout << " a standard exception was caught, with message '" << e.what() << "'\n";
    }
 
}

Output:

Throwing an integer exception...
 the integer exception was caught, with value: 42
Creating a vector of size 5...
Accessing the 11th element of the vector...
 a standard exception was caught, with message 'out_of_range'