This is cited from Stroustrup third edition

void f () throw (x2 , x3 )
{
  // stuff
}
is equivalent to:
void f ()
try
{
  // stuff
}

catch (x2) {throw; } / / rethrow
catch (x3) {throw; } / / rethrow
catch (...) {
std: :unexpected() ; / / unexpected() will not return
}

Now take a look at the following code:

#include <iostream>
#include <string>

using namespace std;

class MyFEx
{
	string exMsg_;
public:
	MyFEx( string exMsg ): exMsg_(exMsg) {}
	void print() { cout<<endl<<exMsg_<<endl; } 	
};

class MySEx
{
	string exMsg_;
public:
	MySEx( string exMsg ): exMsg_(exMsg) {}
	void print() { cout<<endl<<exMsg_<<endl;  }	
};

void A(int x) throw ( MyFEx , MySEx )
{
	x < 0 ? throw MyFEx( "myfirst " ) : throw MySEx( "mysecond" ); 
}

void B(int x) throw ( MySEx )
{
	A(x);
}

void MyUnexpectedHandler()
{
	cout<<"OK, HANDLER CALLED"<<endl;
}


int main( int argc, char* argv[] )
{
	unexpected_handler theOldOne = set_unexpected( MyUnexpectedHandler );
	
	
	try 
	{
		B(-1);
	}
	catch(MyFEx& ex)
	{
		ex.print();
	}
	catch( MySEx& ex)
	{
		ex.print();
	}
	
	cout<<"ok, got here"<<endl;

	int i;
	cin>>i;
}

Ok, as I noticed the MSVC compiler, doesn't respect the standard, so don't bother compiling with it. It will compile but the result is that MyFEx will be cought in main althow B doesn't specify that it might throw a MyFEx, and the std::unexpected() handler is not even called.

If compiled with gcc the result is almost what I excpected:
B doesn't specify that it might throw a MyFEx exception so the std::unexpected is called with my handler. All my handler does is print a message. What I don't understand is why after the hander is executed std::terminate is still called?
If things would go as Stroustrup says B woudl be equivalent to:

void B(int x) 
{
       try{
	   A(x);
       }
       catch( MySEx )
       {
            throw;
        }
       catch(...)
        {
              std::unexpected();
         }

}

since my implementation of the unexpected handler doesn't terminate the program B should return and execution of main should continue normal. Can someone plz explain this strange behaviour?

thx in advance.

Recommended Answers

All 2 Replies

From C++ Standard:

15.5.2 The std::unexpected() function [except.unexpected]
1 If a function with an exception-specification throws an exception that is not listed in the exception-specification, the function std::unexpected() is called (18.6.2) immediately after completing the stack unwinding for the former function
2 The std::unexpected() function shall not return, but it can throw (or re-throw) an exception. If it throws a new exception which is allowed by the exception specification which previously was violated, then the search for another handler will continue at the call of the function whose exception specification was violated. If it throws or rethrows an exception that the exception-specification does not allow then the following happens: If the exception-specification does not include the class std::bad_exception (18.6.2.1) then the function std::terminate() is called, otherwise the thrown exception is replaced by an implementation-defined object of the type std::bad_exception and the search for another handler will continue at the call of the function whose exception-specification was violated.
3 Thus, an exception-specification guarantees that only the listed exceptions will be thrown.
...

and another quote:

18.6.2.2 Type unexpected_handler [lib.unexpected.handler]
typedef void (* unexpected_handler )();
1 The type of a handler function to be called by unexpected() when a function attempts to throw an exception not listed in its exception-specification.
2 Required behavior: An unexpected_handler shall not return. See also 15.5.2.
3 Default behavior: The implementation’s default unexpected_handler calls terminate().

May be, it helps.

From C++ Standard:

and another quote:

May be, it helps.

yes, it does :P, thx.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.