exception, constructor and return value

N

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(...)
{
}

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?
 
A

Alf P. Steinbach

* 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).
 
N

Neelesh Bodas

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.
 
A

Alf P. Steinbach

* 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.
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top