473,473 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

exceptions and stack objects

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
Oct 7 '05 #1
6 1652
John Ratliff wrote:
What happens to objects created on the stack when you throw an
exception?


Destroyed in the process of "stack unwinding".

V
Oct 7 '05 #2
John Ratliff wrote:
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
Oct 7 '05 #3
Kai-Uwe Bux wrote:
John Ratliff wrote:

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.


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
Oct 7 '05 #4
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.
Oct 7 '05 #5
Ron Natalie wrote:
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.


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
Oct 8 '05 #6
"Kai-Uwe Bux" <jk********@gmx.net> wrote in message news:di**********@murdoch.acc.Virginia.EDU...
Ron Natalie wrote:
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.


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.
Oct 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: David Turner | last post by:
Hi all I noticed something interesting while testing some RAII concepts ported from C++ in Python. I haven't managed to find any information about it on the web, hence this post. The problem...
9
by: Magalie | last post by:
Hi! I've got a question concerning C++ exceptions, more precisely the stack unwinding when an exception occurs. We agree that when an exception is thrown, all objects that have destructors are...
12
by: Siemel Naran | last post by:
(1) About std::exit. If I call this function, will the system call the destructors of all objects in the call stack. Eg. void f() { std::exit(1); } void g() { X x; f(); } Does the call in...
9
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned...
59
by: kk_oop | last post by:
Hi. I wanted to use exceptions to handle error conditions in my code. I think doing that is useful, as it helps to separate "go" paths from error paths. However, a coding guideline has been...
15
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions...
14
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a...
42
by: Jon Harrop | last post by:
Why are exceptions in C++ ~6x slower than exceptions in OCaml? -- Dr Jon D Harrop, Flying Frog Consultancy Objective CAML for Scientists...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.