J
John Ratliff
What happens to objects created on the stack when you throw an exception?
For example, suppose I opened a file for reading with an fstream. If I
perform a read operation and I don't like the result, is it okay to
throw an exception? If I do, will the fstreams destructor get called?
Will the file get closed?
Something like:
void method() {
char buffer[0x2000];
std::fstream
in("smetroid.srm", std::ios_base::in | std::ios_base::binary);
in.read(buffer, 0x2000);
if (in.gcount() != 0x2000) {
// not a valid file
throw FooException();
}
in.close();
}
And yes, I know the final call to close is not necessary.
What will happen if FooException gets thrown? Assume it's just a
derivative of std::runtime_error.
Thanks,
--John Ratliff
For example, suppose I opened a file for reading with an fstream. If I
perform a read operation and I don't like the result, is it okay to
throw an exception? If I do, will the fstreams destructor get called?
Will the file get closed?
Something like:
void method() {
char buffer[0x2000];
std::fstream
in("smetroid.srm", std::ios_base::in | std::ios_base::binary);
in.read(buffer, 0x2000);
if (in.gcount() != 0x2000) {
// not a valid file
throw FooException();
}
in.close();
}
And yes, I know the final call to close is not necessary.
What will happen if FooException gets thrown? Assume it's just a
derivative of std::runtime_error.
Thanks,
--John Ratliff