#include <iostream>
#include <memory>
#include <stdexcept>
class Resource {
public:
Resource(int id) : id_(id) {
std::cout << "Resource " << id_ << " acquired\n";
}
~Resource() {
std::cout << "Resource " << id_ << " released\n";
}
private:
int id_;
};
class WeakExceptionGuarantee {
private:
std::unique_ptr<Resource> resource1;
std::unique_ptr<Resource> resource2;
public:
void operation() {
resource1 = std::make_unique<Resource>(1);
// Simulating some work that might throw
if (rand() % 2 == 0) {
throw std::runtime_error("Simulated error");
}
resource2 = std::make_unique<Resource>(2);
}
};
int main() {
WeakExceptionGuarantee weg;
try {
weg.operation();
} catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << std::endl;
}
return 0;
}