C++ catch Keyword

The catch keyword in C++ is used in exception handling to define a block of code that handles exceptions. When an exception is thrown using the throw keyword, the program searches for a matching catch block to handle the exception. If a matching catch block is found, its code executes, allowing the program to recover from the error or terminate gracefully.

The catch block is part of the try-catch mechanism in C++ and is typically used to manage runtime errors, ensuring robust and error-resilient programs.


Syntax

</>
Copy
try {
    // Code that might throw an exception
} catch (exception_type variable_name) {
    // Code to handle the exception
}
try
The block of code where exceptions might be thrown.
exception_type
The type of exception to catch, such as int, std::exception, or a custom type.
variable_name
An optional name for the caught exception, which can be used within the catch block.

Examples

Example 1: Basic Exception Handling

This example demonstrates a basic use of the try and catch keywords to handle an exception.

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

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

Output:

Caught an exception: 10

Explanation:

  1. The try block contains code that throws an integer exception using the throw keyword.
  2. The catch block catches the exception of type int and stores its value in the variable e.
  3. The value of e is printed, displaying the caught exception.

Example 2: Catching Exceptions of Different Types

This example shows how multiple catch blocks can handle exceptions of different types.

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

int main() {
    try {
        throw "An error occurred"; // Throw a string exception
    } catch (const char* e) {
        cout << "Caught a string exception: " << e << endl;
    } catch (...) {
        cout << "Caught an unknown exception" << endl;
    }
    return 0;
}

Output:

Caught a string exception: An error occurred

Explanation:

  1. The try block throws a string exception of type const char*.
  2. The first catch block catches exceptions of type const char* and prints the error message.
  3. The second catch block with ... acts as a catch-all, handling any exception type not explicitly caught by previous catch blocks.

Example 3: Rethrowing an Exception

This example demonstrates how an exception can be rethrown from a catch block.

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

void testException() {
    try {
        throw 42; // Throw an integer exception
    } catch (int e) {
        cout << "Caught in testException: " << e << endl;
        throw; // Rethrow the exception
    }
}

int main() {
    try {
        testException();
    } catch (int e) {
        cout << "Caught in main: " << e << endl;
    }
    return 0;
}

Output:

Caught in testException: 42
Caught in main: 42

Explanation:

  1. The function testException throws an exception, which is caught in its own catch block.
  2. The exception is rethrown using the throw; statement, allowing it to propagate to the calling code.
  3. The catch block in main catches the rethrown exception and handles it.

Key Points about catch Keyword

  1. The catch keyword is used to handle exceptions thrown from a try block.
  2. Multiple catch blocks can be used to handle different exception types.
  3. The ... in a catch block acts as a catch-all for any exception type.
  4. Exceptions can be rethrown from a catch block using the throw; statement.