https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61372

--- Comment #10 from R. Diez <rdiez-2006 at rd10 dot de> ---
I have been bitten by this lack of warning before, but GCC 10.5 should be able
to issue such a warning by default and has the following option in order to
disable the warning:

-Wno-terminate (C++ and Objective-C++ only)
Disable the warning about a throw-expression that will immediately result in a
call to terminate.

However, GCC 10.5 did not issue the warning for me, but GCC 11.4 (and upwards)
did. This is the test case I used:

void test ( void ) throw()
{
  throw 123;
}

The warning is:

warning: 'throw' will always call 'terminate' [-Wterminate]

That test source is available here to play with:

https://godbolt.org/z/8oxbvPEPf

Bug 94112 has been marked as a duplicate of this one, but an important piece of
information is missing here: the warning above only works at the point where an
exception is thrown.

Like bug 94112 mentions, if a nothrow routine calls another one which can
throw, no warning is generated.

I believe that such a warning is important and I do not agree that it would
likely be far too noisy to be useful. Such a situation is an accident waiting
to happen. If in doubt, you can always disable the warning by default, but I
would always try to enable it for my software.

I also believe that, when you use extern "C", you are mostly thinking about
interfacing with C code which does not know about exceptions and does not
expect the execution order to suddenly change. The C code may then miss some
clean-up logic before the stack is unwound.

That is why I think that a warning for extern "C" routines is also important.
In the few scenarios where an extern "C" routine should support exceptions, you
could always disable the warning with a #pragma.

Reply via email to