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
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
catchblock.
Examples
Example 1: Basic Exception Handling
This example demonstrates a basic use of the try and catch keywords to handle an exception.
#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:
- The
tryblock contains code that throws an integer exception using thethrowkeyword. - The
catchblock catches the exception of typeintand stores its value in the variablee. - The value of
eis 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.
#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:
- The
tryblock throws a string exception of typeconst char*. - The first
catchblock catches exceptions of typeconst char*and prints the error message. - The second
catchblock with...acts as a catch-all, handling any exception type not explicitly caught by previouscatchblocks.
Example 3: Rethrowing an Exception
This example demonstrates how an exception can be rethrown from a catch block.
#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:
- The function
testExceptionthrows an exception, which is caught in its owncatchblock. - The exception is rethrown using the
throw;statement, allowing it to propagate to the calling code. - The
catchblock inmaincatches the rethrown exception and handles it.
Key Points about catch Keyword
- The
catchkeyword is used to handle exceptions thrown from atryblock. - Multiple
catchblocks can be used to handle different exception types. - The
...in acatchblock acts as a catch-all for any exception type. - Exceptions can be rethrown from a
catchblock using thethrow;statement.
