Exceptions can be caught and re-thrown to be handled by higher level functions in C++ using the
throw keyword. The throw keyword is used to throw an exception. The exception can be any object, but it is usually a
std::exception object or a derived class of std::exception.
To catch and re-throw an exception, you can use the catch statement. The
catch statement specifies the type of exception that you want to catch. If an exception of the specified type is thrown, the
catch statement will be executed.
The following code shows how to catch and re-throw an exception:
In this code, the foo() function throws a std::runtime_error exception. The
bar() function catches this exception and then re-throws it. The
main() function catches the exception and prints a message to the console.
It is important to note that the re-thrown exception will be caught by the first
catch statement that matches its type. If there is no catch statement that matches the type of the exception, the program will terminate.
Here are some additional things to keep in mind when catching and re-throwing exceptions:
You can catch multiple exceptions in a single catch statement by using the
| operator. For example, the following code catches both std::runtime_error and
std::logic_error exceptions:
C++
catch (std::runtime_error | std::logic_error e) {
You can also catch a wildcard exception by using the catch (...) statement. The
catch (...) statement will catch any exception, regardless of its type.
It is important to handle all exceptions that are thrown by your code. If an exception is not handled, the program will terminate.
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.
Exceptions can be caught and re-thrown to be handled by higher level functions in C++ using the
throwkeyword. Thethrowkeyword is used to throw an exception. The exception can be any object, but it is usually astd::exceptionobject or a derived class ofstd::exception.To catch and re-throw an exception, you can use the
catchstatement. Thecatchstatement specifies the type of exception that you want to catch. If an exception of the specified type is thrown, thecatchstatement will be executed.The following code shows how to catch and re-throw an exception:
C++
In this code, the
foo()function throws astd::runtime_errorexception. Thebar()function catches this exception and then re-throws it. Themain()function catches the exception and prints a message to the console.It is important to note that the
re-thrownexception will be caught by the firstcatchstatement that matches its type. If there is nocatchstatement that matches the type of the exception, the program will terminate.Here are some additional things to keep in mind when catching and re-throwing exceptions:
catchstatement by using the|operator. For example, the following code catches bothstd::runtime_errorandstd::logic_errorexceptions:C++
You can also catch a wildcard exception by using the
catch (...)statement. Thecatch (...)statement will catch any exception, regardless of its type.It is important to handle all exceptions that are thrown by your code. If an exception is not handled, the program will terminate.