try
BlockThe general form of a try
block is
try {
program-statements
} catch (exception-declaration) {
handler-statements
} catch (exception-declaration) {
handler-statements
} // . . .
A try
block begins with the keyword try
followed by a block, which, as usual, is a sequence of statements enclosed in curly braces.
Following the try
block is a list of one or more catch
clauses. A catch
consists of three parts: the keyword catch
, the declaration of a (possibly unnamed) object within parentheses (referred to as an exception declaration), and a block. When a catch
is selected to handle an exception, the associated block is executed. Once the catch
finishes, execution continues with the statement immediately following the last catch
clause of the try
block.
The program-statements inside the try
constitute the normal logic of the program. Like any other blocks, they can contain any C++ statement, including declarations. As with any block, variables declared inside a try
block are inaccessible outside the block—in particular, they are not accessible to the catch
clauses.
In the preceding example, we used a throw
to avoid adding two Sales_item
s that represented different books. We imagined that the part of the program that added two Sales_item
s was separate from the part that communicated with the user. The part that interacts with the user might contain code something like the following to handle the exception that was thrown:
while (cin >> item1 >> item2) {
try {
// execute code that will add the two Sales_items
// if the addition fails, the code throws a runtime_error exception
} catch (runtime_error err) {
// remind the user that the ISBNs must match and prompt for another pair
cout << err.what()
<< "
Try Again? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
break; // break out of the while loop
}
}
The ordinary logic of the program that manages the interaction with the user appears inside the try
block. This part of the program is wrapped inside a try
because it might throw an exception of type runtime_error
.
This try
block has a single catch
clause, which handles exceptions of type runtime_error
. The statements in the block following the catch
are executed if code inside the try
block throws a runtime_error
. Our catch
handles the error by printing a message and asking the user to indicate whether to continue. If the user enters ’n
’, then the break
is executed and we exit the while
. Otherwise, execution falls through to the closing brace of the while
, which transfers control back to the while
condition for the next iteration.
The prompt to the user prints the return from err.what()
. We know that err
has type runtime_error
, so we can infer that what
is a member function (§ 1.5.2, p. 23) of the runtime_error
class. Each of the library exception classes defines a member function named what
. These functions take no arguments and return a C-style character string (i.e., a const char*
). The what
member of runtime_error
returns a copy of the string
used to initialize the particular object. If the code described in the previous section threw an exception, then this catch
would print
Data must refer to same ISBN
Try Again? Enter y or n
In complicated systems, the execution path of a program may pass through multiple try
blocks before encountering code that throws an exception. For example, a try
block might call a function that contains a try
, which calls another function with its own try
, and so on.
The search for a handler reverses the call chain. When an exception is thrown, the function that threw the exception is searched first. If no matching catch
is found, that function terminates. The function that called the one that threw is searched next. If no handler is found, that function also exits. That function’s caller is searched next, and so on back up the execution path until a catch
of an appropriate type is found.
If no appropriate catch
is found, execution is transferred to a library function named terminate
. The behavior of that function is system dependent but is guaranteed to stop further execution of the program.
Exceptions that occur in programs that do not define any try
blocks are handled in the same manner: After all, if there are no try
blocks, there can be no handlers. If a program has no try
blocks and an exception occurs, then terminate
is called and the program is exited.