A
Adam H. Peterson
Is there a way (idiom, construct, anything like that) to divorce the
scope of a try block from the region of code where it will catch the
exception? I mean, suppose I have a code snippet like this:
try {
Object o("data", 25, 16384);
o.process(); // This may throw, but I don't want to handle it here.
} catch (...) {
std::cerr << "Couldn't construct the object" << std::endl;
}
Now, suppose the construction of o could fail with an exception, which
is why we put it inside a try block. If this happens, we want to do
something to handle it (in this case, notify the user and move on). But
after it's constructed, we want to do something else with the object,
and this stuff could also fail. But if it does, we want the exception
to propagate outward to the surrounding handlers.
Is there a way to trap exceptions from a constructor and not trap
exceptions in the subsequent processing of the object?
scope of a try block from the region of code where it will catch the
exception? I mean, suppose I have a code snippet like this:
try {
Object o("data", 25, 16384);
o.process(); // This may throw, but I don't want to handle it here.
} catch (...) {
std::cerr << "Couldn't construct the object" << std::endl;
}
Now, suppose the construction of o could fail with an exception, which
is why we put it inside a try block. If this happens, we want to do
something to handle it (in this case, notify the user and move on). But
after it's constructed, we want to do something else with the object,
and this stuff could also fail. But if it does, we want the exception
to propagate outward to the surrounding handlers.
Is there a way to trap exceptions from a constructor and not trap
exceptions in the subsequent processing of the object?