472,954 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

exception, constructor and return value

Hello all,
Just wanted a small clarification on these two points :

1) The following code compiles well -

int f() try {
throw "xyz";
}
catch(...)
{
}

What is expected to be the return value (I am asking about _value_, not
_type_ ) of f()? Is it undefined , implementation defined or std
defined?
Basically what happens to the return value if a function-level try
block throws an exception and the exception is caught at the same level
(and there is no return statement anywhere)?

2) When a ctor throws an exception and the exception is handelled at
the same level, does that mean that the object gets constructed
completely?

#include <iostream>
class X {
public:
X() try
{
throw 100;
}
catch(...)
{
}

};

int main()
{
X x;
std::cout << "main" << std::endl;
// I observe that this line doesnot get executed even when the
exception is caught in the ctor itself.
//I also see that the this exception gets caught in the catch(...)
handler of the constructor AND it _also_ gets propogated to main, and
it _can again_ be caught by putting the necessary catch block in main
!!!

}
What is the exact reason?

Nov 22 '05 #1
3 1451
* Neelesh Bodas:
Hello all,
Just wanted a small clarification on these two points :

1) The following code compiles well -

int f() try {
throw "xyz";
}
catch(...)
{
}
UB, same as reaching the end of the function without specifying a return
value.

2) When a ctor throws an exception and the exception is handelled at
the same level, does that mean that the object gets constructed
completely?

#include <iostream>
class X {
public:
X() try
{
throw 100;
}
catch(...)
{
}


Well-defined, the exception is rethrown as per 15.3/17 because this is a
constructor (the same goes for destructors).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #2
Thanks a lot for the help Alf.

On a continuing note, just wanted to know which of these is correct -

1) Whenever a destructor throws an exception (and it is uncaught),
terminate() is called
2) When a destructor throws _during the process of stack unwinding
happening during the process of handling some previously thrown
exception_ , (and the exception thrown by dtor is uncaught) then
terminate() is called.
Thanks ,
Neelesh.
Alf P. Steinbach wrote:
* Neelesh Bodas:
Hello all,
Just wanted a small clarification on these two points :

1) The following code compiles well -

int f() try {
throw "xyz";
}
catch(...)
{
}


UB, same as reaching the end of the function without specifying a return
value.

2) When a ctor throws an exception and the exception is handelled at
the same level, does that mean that the object gets constructed
completely?

#include <iostream>
class X {
public:
X() try
{
throw 100;
}
catch(...)
{
}


Well-defined, the exception is rethrown as per 15.3/17 because this is a
constructor (the same goes for destructors).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Nov 22 '05 #3
* Neelesh Bodas:
[top-posting]
[quoting extranous material]
Please don't top-post in this group (or any non-Microsoft group);
see the FAQ for this group as well as general Usenet guidelines.

And please don't quote things you're not responding to.
* Neelesh Bodas:
On a continuing note, just wanted to know which of these is correct -

1) Whenever a destructor throws an exception (and it is uncaught),
terminate() is called
2) When a destructor throws _during the process of stack unwinding
happening during the process of handling some previously thrown
exception_ , (and the exception thrown by dtor is uncaught) then
terminate() is called.


When there is no suitable handler for an exception, std::terminate is
called; that's got nothing to do with destructors or stack unwinding.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #4

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

Similar topics

27
by: garyolsen | last post by:
In C++ what kind of unexpected conditions should be handled as exceptions? Besides dividing by 0, bad memory allocation, what're the most popular exceptions? When should not use exception,...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
7
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass...
4
by: JSheble | last post by:
If an exception occurs or is thrown in my constructor, what does the constructor actually return? A null?
11
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all...
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
4
by: George2 | last post by:
Hello everyone, Here is Bjarne's exception safe sample, http://www.research.att.com/~bs/3rd_safe.pdf template <class Tclass Safe {
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.