The try Statement
To handle an exception, place any code that might throw an exception in a try statement (lines 77–85). The try block (lines 77–81) contains the code that might throw an exception, and the catch block (lines 82–85) contains the code that handles the exception if one occurs. As you’ll see in Chapter 17, you can have many
catch blocks to handle different types of exceptions that might be thrown in the corresponding
try block. If the code in the
try block executes successfully, lines 82–85 are ignored. The braces that delimit
catch blocks’ bodies are required.
vector member function at provides bounds checking and throws an exception if its argument is an invalid subscript. By default, this causes a C++ program to terminate. ...