Exception safety refers to the ability of code to correctly handle and recover from exceptions without leaving the program in an inconsistent or invalid state. Exception-safe code ensures that resources are properly managed, invariants are maintained, and the program remains in a well-defined state, even in the presence of exceptions.
There are three commonly recognized levels of exception safety:
To ensure exception safety in your code, you can follow these guidelines:
std::unique_ptr
, std::shared_ptr
) to manage dynamically allocated memory and avoid manual memory management.noexcept
specifiers to indicate functions that are guaranteed not to throw exceptions.Here's an example of exception-safe code using RAII:
#include <iostream>
#include <memory>
class Resource {
public:
Resource() {
std::cout << "Acquiring resource\n";
}
~Resource() {
std::cout << "Releasing resource\n";
}
};
void foo() {
std::unique_ptr<Resource> resource{
std::make_unique<Resource>()};
// Use the resource
throw std::runtime_error("Exception occurred");
}
int main() {
try {
foo();
} catch (const std::exception& ex) {
std::cout << "Caught exception: "
<< ex.what();
}
}
Acquiring resource
Releasing resource
Caught exception: Exception occurred
In this example, the Resource
class represents a resource that needs to be acquired and released. The std::unique_ptr
is used to manage the lifetime of the resource. Even if an exception is thrown within the foo()
function, the destructor of Resource
will be called automatically when the std::unique_ptr
goes out of scope, ensuring proper cleanup and preventing resource leaks.
By following exception safety principles and using appropriate techniques like RAII and smart pointers, you can write robust and exception-safe code in C++.
Answers to questions are automatically generated and may not have been reviewed.
throw
, try
and catch
This lesson provides an introduction to exceptions, detailing the use of throw
, try
, and catch
.