Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exception Specifications

3 views
Skip to first unread message

Keith Halligan

unread,
May 2, 2008, 6:43:30 AM5/2/08
to
I'm a bit unsure about the following behaviour with exception
specifiers in C++ functions


Here's some sample code:

#include <exception>

class SysException { };

void f( ) throw (SysException)
{
throw std::exception();
}


int main()
{

}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Martin T

unread,
May 2, 2008, 2:48:22 PM5/2/08
to
Keith Halligan wrote:
> I'm a bit unsure about the following behaviour with exception
> specifiers in C++ functions
>
>
> Here's some sample code:
>
> #include <exception>
>
> class SysException { };
>
> void f( ) throw (SysException)
> {
> throw std::exception();
> }
>
>
> int main()
> {
>
> }
>

Calling f() should result in std::unexpected being called ...

[http://www.devx.com/tips/Tip/15140]
The Standard Library defines a function called std::unexpected() which
is invoked when a function throws an exception not listed in its
exception specification. std::unexpected invokes a user-defined function
that was registered by std::set_unexpected. If the user hasn't
registered such a function, unexpected() will invoke std::terminate(),
which in turn calls abort() and terminates the program. (...)

Exception specifications are apparently evil:
http://www.gotw.ca/publications/mill22.htm

br,
Martin

peter koch larsen

unread,
May 2, 2008, 2:46:50 PM5/2/08
to
On 2 Maj, 12:43, Keith Halligan <keith.halli...@gmail.com> wrote:
> I'm a bit unsure about the following behaviour with exception
> specifiers in C++ functions
>
> Here's some sample code:
>
> #include <exception>
>
> class SysException { };
>
> void f( ) throw (SysException)
> {
> throw std::exception();
>
> }
>
> int main()
> {
>
> }
That program is not very interesting. It will compile and when run
return 0. If you call f in main, std::unexpected will be called and
your program will be terminated. I do not remembet the exact details -
look them up needed.

/Peter

Francis Glassborow

unread,
May 2, 2008, 2:46:16 PM5/2/08
to
Keith Halligan wrote:
> I'm a bit unsure about the following behaviour with exception
> specifiers in C++ functions
>
>
> Here's some sample code:
>
> #include <exception>
>
> class SysException { };
>
> void f( ) throw (SysException)
> {
> throw std::exception();
> }
>
>
> int main()
> {
>
> }
>
calling f() would result in (IIRC) unexpected() being called. The only
exception objects that can propagate from f() are SysException and
instances of types derived from SysException.

--
Note that robinton.demon.co.uk addresses are no longer valid.

Jon

unread,
May 2, 2008, 2:47:05 PM5/2/08
to
On May 2, 4:43 am, Keith Halligan <keith.halli...@gmail.com> wrote:
> I'm a bit unsure about the following behaviour with exception
> specifiers in C++ functions
>
> Here's some sample code:
>
> #include <exception>
>
> class SysException { };
>
> void f( ) throw (SysException)
> {
> throw std::exception();
>
> }

I may be wrong, but I believe that this code should fail to compile.
The exception specification for f() indicates that it will throw
objects only of type SysException. class SysException is not derived
from std::exception so throwing an object of class std::exception
would violate the exception specification.

Regards,

Jon Trauntvein

gnuyuva

unread,
May 2, 2008, 2:51:23 PM5/2/08
to
On May 2, 3:43 pm, Keith Halligan <keith.halli...@gmail.com> wrote:
> I'm a bit unsure about the following behaviour with exception
> specifiers in C++ functions
>
> Here's some sample code:
>
> #include <exception>
>
> class SysException { };
>
> void f( ) throw (SysException)
> {
> throw std::exception();
> }
>

If you 'try' f(), then you can't 'catch' the std::exception.
I think such constructs should be warned by the compilers but my
gcc is happy with this stuff.

Maybe,

void f() throw (SysException, std::exception)
{ throw std::exception(); }

is what you needed.

Greg Herlihy

unread,
May 3, 2008, 8:13:28 AM5/3/08
to
On May 2, 11:47 am, Jon <j.trauntv...@comcast.net> wrote:
> On May 2, 4:43 am, Keith Halligan <keith.halli...@gmail.com> wrote:
>
> > I'm a bit unsure about the following behaviour with exception
> > specifiers in C++ functions
>
> > Here's some sample code:
>
> > #include <exception>
>
> > class SysException { };
>
> > void f( ) throw (SysException)
> > {
> > throw std::exception();
>
> > }
>
> I may be wrong, but I believe that this code should fail to compile.

No. On the contrary, a C++ compiler is not allowed to report an error
with f()'s definition above: According to the C++ Standard:

"An implementation shall not reject an expression merely because when
executed it throws or might throw an exception that the containing
function does not allow." [§15.4/10]

The fact that exception specifications are enforced only at runtime in
C++ (instead of at compile-time when such checking would be useful) -
makes exception specifications too risky for most real-world C++
applications to adopt.

Greg

Francis Glassborow

unread,
May 3, 2008, 6:10:23 PM5/3/08
to
Jon wrote:
> On May 2, 4:43 am, Keith Halligan <keith.halli...@gmail.com> wrote:
>> I'm a bit unsure about the following behaviour with exception
>> specifiers in C++ functions
>>
>> Here's some sample code:
>>
>> #include <exception>
>>
>> class SysException { };
>>
>> void f( ) throw (SysException)
>> {
>> throw std::exception();
>>
>> }
>
> I may be wrong, but I believe that this code should fail to compile.
> The exception specification for f() indicates that it will throw
> objects only of type SysException. class SysException is not derived
> from std::exception so throwing an object of class std::exception
> would violate the exception specification.
>

No, the compiler should compile it because there is no error until f()
is called, and even then there is a well-defined action to be carried
out (call unexpected()) and so the code is still not erroneous.


--
Note that robinton.demon.co.uk addresses are no longer valid.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]

0 new messages