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:
- The
try
block contains a division operation that checks if the divisor is zero. - If the condition is true, an exception is thrown using the
throw
statement. - 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:
- The
try
block throws an integer exception with the value404
. - The first
catch
block matches exceptions of typeint
and handles the exception. - 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:
- The
try
block throws a string exception. - The catch-all handler (
catch (...)
) catches the exception regardless of its type. - This approach is useful for handling unexpected exceptions.
Key Points to Remember about try
Keyword
- The
try
block contains code that might throw exceptions. - Each
try
block must be followed by one or morecatch
blocks. - Exceptions thrown within the
try
block are matched to a correspondingcatch
block by type. - A catch-all handler (
catch (...)
) can be used to catch any exception type.