The try-catch block is a control flow statement in C++ that allows you to handleexceptions. An exception is an event that occurs during the execution of a program that disrupts the normal flow of the program.
The try-catch block consists of three parts:
The try block: This is the block of code that you want to execute. If an exception occurs in the try block, the control flow will be transferred to the catch block.
The catch block: This is the block of code that you want to execute if an exception occurs in the try block. The catch block must specify the type of exception that it can handle.
The finally block: This is the block of code that is always executed, regardless of whether or not an exception occurs in the try block.
The following code shows how to use the try-catch block to handle an exception:
C++
int main() {
try {
int x = 10;
int y = 0;
int z = x / y; // This will cause an exception because y is 0
} catch (const std::exception& e) {
std::cout << "An exception occurred: " << e.what() << std::endl;
} finally {
std::cout << "This code is always executed." << std::endl;
}
return 0;
}
In this code, the try block contains the code that may throw an exception. The catch block catches the exception and prints a message to the console. The finally block is always executed, regardless of whether or not an exception occurs.
The std::exception class is the base class for all exceptions in C++. If you want to handle a specific type of exception, you can use a catch block that specifies the type of exception. For example, the following code shows how to handle a
std::overflow_error exception:
C++
try {
int x = 10;
int y = 0;
int z = x / y; // This will cause an exception because y is 0
} catch (const std::overflow_error& e) {
std::cout << "An overflow error occurred: " << e.what() << std::endl;
} finally {
std::cout << "This code is always executed." << std::endl;
}
The std::overflow_error class is a subclass of std::exception that is thrown when an arithmetic operation overflows. In this code, the catch block will only handle
std::overflow_error exceptions. If another type of exception occurs, the catch block will not be executed.
The try-catch block is a powerful tool for handling exceptions in C++. It allows you to gracefully handle errors and continue the execution of your program.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The try-catch block is a control flow statement in C++ that allows you to handle exceptions. An exception is an event that occurs during the execution of a program that disrupts the normal flow of the program.
The try-catch block consists of three parts:
The following code shows how to use the try-catch block to handle an exception:
C++
In this code, the try block contains the code that may throw an exception. The catch block catches the exception and prints a message to the console. The finally block is always executed, regardless of whether or not an exception occurs.
The
std::exceptionclass is the base class for all exceptions in C++. If you want to handle a specific type of exception, you can use a catch block that specifies the type of exception. For example, the following code shows how to handle astd::overflow_errorexception:C++
The
std::overflow_errorclass is a subclass ofstd::exceptionthat is thrown when an arithmetic operation overflows. In this code, the catch block will only handlestd::overflow_errorexceptions. If another type of exception occurs, the catch block will not be executed.The try-catch block is a powerful tool for handling exceptions in C++. It allows you to gracefully handle errors and continue the execution of your program.