exceptions and stack objects

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
 
K

Kai-Uwe Bux

John said:
What happens to objects created on the stack when you throw an exception?

Provided you catch the exception, the program will destroy the objects
during stack unwinding.

If you do not catch the exception control is not transfered to an exception
handler and stack unwindind, I think, does not happen.


Best

Kai-Uwe Bux
 
J

John Ratliff

Kai-Uwe Bux said:
John Ratliff wrote:




Provided you catch the exception, the program will destroy the objects
during stack unwinding.

If you do not catch the exception control is not transfered to an exception
handler and stack unwindind, I think, does not happen.

Well, I don't think it matters what happens if I don't catch the
exception, cause then the program will crash.

But I plan to catch this exception if it ever happens.

Thanks,

--John Ratliff
 
R

Ron Natalie

If you do not catch the exception control is not transfered to an exception
handler and stack unwindind, I think, does not happen.

It's actually unspecified if it happens or not. It might or might not.
 
K

Kai-Uwe Bux

Ron said:
It's actually unspecified if it happens or not. It might or might not.

Is this unspecified or undefined behavior? If it is unspecified, could you
give me direct me to a clause in the standard where it "unspecifies" the
behavior?

I am asking because I wonder how the compiler would, in absence of a
matching catch statement, determine where the enclosing block is and where
to stop the unwinding process. Or is it required to proceed all the way
back to main()? If so, how is that implied by the standard. (I recently
tried to figure out what happens if you do not catch an exception, and it
was a real pain. Obviously, I took a wrong turn somewhere since I convinced
myself that stack unwinding is triggered by transfer of control to a
matching exception handler which requires a catch. Oh well.)


Best

Kai-Uwe Bux
 
F

Fred Zwarts

Kai-Uwe Bux said:
Is this unspecified or undefined behavior? If it is unspecified, could you
give me direct me to a clause in the standard where it "unspecifies" the
behavior?

I am asking because I wonder how the compiler would, in absence of a
matching catch statement, determine where the enclosing block is and where
to stop the unwinding process. Or is it required to proceed all the way
back to main()?

Stack unwinding will not always end in main(). Examples:

1) Exceptions in a multi-threaded program.
2) Exceptions in a constructor of a static variable declared outside the scope of main.

F.Z.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top