By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,754 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

exception, constructor and return value

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
* 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

P: n/a
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

P: n/a
* 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 discussion thread is closed

Replies have been disabled for this discussion.