C++ try Keyword

The try keyword in C++ is used to define a block of code that is monitored for exceptions. It is part of the exception handling mechanism in C++ and works alongside the catch and throw keywords to manage runtime errors.

The try block contains the code that may generate an exception, and any exceptions thrown within it are caught by the corresponding catch block. This helps in isolating error-prone code and providing specific error-handling mechanisms.


Syntax

</>
Copy
try {
    // Code that may throw an exception
} catch (exception_type variable) {
    // Code to handle the exception
}
try
Defines a block of code to be monitored for exceptions.
catch
Handles exceptions thrown by the try block.
exception_type
The type of the exception to catch (e.g., int, std::exception, or custom types).

Examples

Example 1: Basic Exception Handling with try and catch

This example demonstrates how to use a try block to monitor a code segment and a catch block to handle an exception.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    try {
        int divisor = 0;
        if (divisor == 0) {
            throw "Division by zero error"; // Throw an exception
        }
    } catch (const char* e) {
        cout << "Caught exception: " << e << endl;
    }
    return 0;
}

Output:

Caught exception: Division by zero error

Explanation:

  1. The try block contains a division operation that checks if the divisor is zero.
  2. If the condition is true, an exception is thrown using the throw statement.
  3. The catch block catches the exception and handles it by printing an error message.

Example 2: Handling Multiple Exceptions

Multiple catch blocks can be used to handle different types of exceptions.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    try {
        throw 404; // Throw an integer exception
    } catch (int e) {
        cout << "Caught integer exception: " << e << endl;
    } catch (const char* e) {
        cout << "Caught string exception: " << e << endl;
    }
    return 0;
}

Output:

Caught integer exception: 404

Explanation:

  1. The try block throws an integer exception with the value 404.
  2. The first catch block matches exceptions of type int and handles the exception.
  3. The second catch block is ignored because the exception has already been handled.

Example 3: Catch-All Handler

The catch-all handler (catch (...)) can be used to catch exceptions of any type.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    try {
        throw "Unknown error occurred"; // Throw a string exception
    } catch (...) {
        cout << "Caught an exception of unknown type" << endl;
    }
    return 0;
}

Output:

Caught an exception of unknown type

Explanation:

  1. The try block throws a string exception.
  2. The catch-all handler (catch (...)) catches the exception regardless of its type.
  3. This approach is useful for handling unexpected exceptions.

Key Points to Remember about try Keyword

  1. The try block contains code that might throw exceptions.
  2. Each try block must be followed by one or more catch blocks.
  3. Exceptions thrown within the try block are matched to a corresponding catch block by type.
  4. A catch-all handler (catch (...)) can be used to catch any exception type.