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
catch
block.
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
try
block contains code that throws an integer exception using thethrow
keyword. - The
catch
block catches the exception of typeint
and stores its value in the variablee
. - 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.
#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
try
block throws a string exception of typeconst char*
. - The first
catch
block catches exceptions of typeconst char*
and prints the error message. - The second
catch
block with...
acts as a catch-all, handling any exception type not explicitly caught by previouscatch
blocks.
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
testException
throws an exception, which is caught in its owncatch
block. - The exception is rethrown using the
throw;
statement, allowing it to propagate to the calling code. - The
catch
block inmain
catches the rethrown exception and handles it.
Key Points about catch
Keyword
- The
catch
keyword is used to handle exceptions thrown from atry
block. - Multiple
catch
blocks can be used to handle different exception types. - The
...
in acatch
block acts as a catch-all for any exception type. - Exceptions can be rethrown from a
catch
block using thethrow;
statement.