Try, Catch, and Throw Statements
The try, catch, and throw statements are the fundamental components of exception handling in C++. They work together to detect, handle, and recover from runtime errors in a program.
1. try Block
The try block contains the code that may potentially throw an exception. If an exception occurs within this block, control is transferred to the catch block that follows the try block.
Syntax:
try {
// Code that may throw an exception
}
Example:
try {
int a = 10;
int b = 0;
if (b == 0) {
throw"Division by zero!";
}
int c = a / b;
}
In this example, the try block contains a division operation that might cause an error (division by zero).
2. throw Statement
The throw statement is used to throw an exception. When an exception is thrown, the normal execution of the program stops, and the control is transferred to the nearest catch block that can handle the exception.
Syntax:
throw exception;
Example:
if (b == 0) {
throw"Division by zero!";
}
In this example, if b is zero, the throw statement is executed, throwing an exception of type const char*.
3. catch Block
The catch block handles the exception that was thrown in the try block. The type of exception that the catch block can handle is specified in parentheses. If the thrown exception matches the type, the catch block is executed.
Syntax:
catch (exception_type variable_name) {
// Code to handle the exception
}
Example:
catch (constchar* msg) {
std::cerr << "Error: " << msg << std::endl;
}
In this example, the catch block handles an exception of type const char* and prints an error message to the standard error output.
Putting It All Together
Here's a complete example that demonstrates the use of try, throw, and catch:
#include<iostream>int main() {
try {
int a = 10;
int b = 0;
if (b == 0) {
throw"Division by zero error!";
}
int c = a / b;
std::cout << "Result: " << c << std::endl;
} catch (constchar* msg) {
std::cerr << "Exception caught: " << msg << std::endl;
}
std::cout << "Program continues after exception handling." << std::endl;
return0;
}
Explanation:
- The try block contains code that attempts to divide a by b.
- If b is zero, a const char* exception is thrown with the message "Division by zero error!".
- The catch block catches this exception and prints the error message.
- The program continues execution after the catch block, demonstrating that the exception was handled gracefully.
Key Points to Remember
Multiple catch Blocks: You can have multiple catch blocks to handle different types of exceptions.
try {
// Code that may throw multiple types of exceptions
} catch (int e) {
// Handle integer exception
} catch (constchar* msg) {
// Handle string exception
}
Catching All Exceptions: You can catch all types of exceptions using a generic catch block.
catch (...) {
std::cerr << "An unknown exception was caught." << std::endl;
}
Re-throwing Exceptions: Inside a catch block, you can re-throw the caught exception to be handled by another catch block higher up in the call stack.
catch (const std::exception& e) {
throw; // Re-throws the exception
}
These constructs provide a robust way to manage errors and ensure that your program can handle unexpected situations gracefully.