public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Warnings for unhandled c++ exceptions?
@ 2003-02-03 21:37 Robert Dewar
  0 siblings, 0 replies; 22+ messages in thread
From: Robert Dewar @ 2003-02-03 21:37 UTC (permalink / raw)
  To: phil, terpstra; +Cc: gcc, gdr, mark

> The FSF and RMS have some psychological hangup with calling code "legal" or
> "illegal".  (My own opinion is that, as long as the ISO language standard
> uses it, RMS can take a leap.)

Note that "legal" is also a very well defined technical term in Ada, and
it would be actively confusing to use any ill-defined term in its place
when discussing Ada programs.

^ permalink raw reply	[flat|nested] 22+ messages in thread
* Re: Warnings for unhandled c++ exceptions?
@ 2003-02-03 21:25 Robert Dewar
  0 siblings, 0 replies; 22+ messages in thread
From: Robert Dewar @ 2003-02-03 21:25 UTC (permalink / raw)
  To: mark, terpstra; +Cc: gcc

I think it is a bad idea to put in this warning. There are good and valid
arguments on both sides of this issue. Ada and C++ have both decided (after
a discussion in which the merits of both sides were fully understood) decided
to go one way, Java (which may or may not have been subject to a similar
carefully considered argument, who knows?) decided to go the other way. It
is inappropriate to have a compiler second guess this decision.

^ permalink raw reply	[flat|nested] 22+ messages in thread
* Re: Warnings for unhandled c++ exceptions?
@ 2003-02-03 19:39 Chris Lattner
  2003-02-03 19:47 ` Wesley W. Terpstra
  0 siblings, 1 reply; 22+ messages in thread
From: Chris Lattner @ 2003-02-03 19:39 UTC (permalink / raw)
  To: Wesley W. Terpstra; +Cc: gcc


> My primary goals for this warning were solely: bug tracking and
> consistency verification. Nothing more.
...
> In a large enough software project, you probably won't be calling many of
> these out-of-project methods (and such calls are suspect anyways), so the
> warnings are still useful in the context you describe.

Remember that one of the major problems with C++ exception specifications
is that they basically can't work with generic code.  For example, if you
had a throw clause on your copy ctor, you couldn't use an std::vector of
your class without getting a warning (because the code calling the copy
ctor doesn't have an exception specification).

Note that this is just an example, even if you don't have throw'ing copy
ctors it can come up in other cases as well... With any amount of generic
code used, I can't imagine how this would be practical at all.

-Chris

-- 
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/

^ permalink raw reply	[flat|nested] 22+ messages in thread
* Re: Warnings for unhandled c++ exceptions?
@ 2003-02-03 19:16 Wesley W. Terpstra
  0 siblings, 0 replies; 22+ messages in thread
From: Wesley W. Terpstra @ 2003-02-03 19:16 UTC (permalink / raw)
  To: gcc

On Mon, Feb 03, 2003 at 04:11:07PM -0200, Alexandre Oliva wrote:
> On Feb  3, 2003, "Wesley W. Terpstra" <terpstra@ito.tu-darmstadt.de> wrote:
> 
> > I suggested an optional flag to be used for compiler-assisted bug-tracking
> > and quality code writing. (refer to the earlier portion of the message)
> 
> /me thinks such a warning would be mostly useless
> 
> I actually find this sad, but if you consider that most of the C++
> code out there, as well as the C library whose declarations are
> brought into C++, have no throw() declarations at all, you'd quickly
> drown into warning due to calling methods that could throw anything
> but that in reality don't, and it would be very difficult to tell what
> warnings are actually significant.

Yes, but you missed the fine print in my options' descriptions.

        -Wthrow-clause-violated
                If a method throws an exception which is not listed in it's
                throw () clause, or calls a method whose throw clause is not
                handled or not included, generate a warning.

(by not included I meant not included in our throw clause)

        -Wthrow-clause-missing
                If a method throws an exception or calls a method which
                declares a throw clause, but has no throw-clause itself,
                a warning is generated.
                Implies -Wthrow-clause-violated

The first one does not warn about functions which might throw but don't
include a throw () clause. Nor does it warn about calling them.

The second will only warn about things defined within your translation unit.
... excepting inline methods of course... and I see that the libstdc++ does
this. Perhaps the above throw-clause-missing should be ignored for inlined
methods.

Therefore, these warnings will NOT be issued by including other people's
no-throw-declared headers. Only when misusing throw () declared methods and
writing bad methods yourself.

Neither warning is triggered by:

void foo(); // defined elsewhere and actually throws ints

void bar()
{
	foo();
}

In a large enough software project, you probably won't be calling many of
these out-of-project methods (and such calls are suspect anyways), so the
warnings are still useful in the context you describe.

---
Wes

----- End forwarded message -----

^ permalink raw reply	[flat|nested] 22+ messages in thread
* Re: Warnings for unhandled c++ exceptions?
@ 2003-02-03 19:14 Wesley W. Terpstra
  0 siblings, 0 replies; 22+ messages in thread
From: Wesley W. Terpstra @ 2003-02-03 19:14 UTC (permalink / raw)
  To: gcc

On Mon, Feb 03, 2003 at 09:05:59AM -0800, Joe Buck wrote:
> On Mon, Feb 03, 2003 at 02:13:45PM +0100, Wesley W. Terpstra wrote:
> > Is there a compiler flag for g++ which would warn on these code snippets:
> > 
> > 1.
> > 
> > void foo()
> > {
> > 	throw 4;
> > }
> > 
> > Warning: Exception 'int' must be caught, or it must be declared in throw ()
> > clause of this method.
> 
> But there is no such requirement in C++.  If there is no throw() directive,
> it means that anything can be thrown.

I understand this, but it would still be useful to have an option to track
bugs. No one is forced to use this option, and those who choose to say:
	'the above code is unethical' can write better code

> > I find this feature of Java remarkably useful as it helps to detect sloppy
> > exception handling, which is brutal to find in C++. I do not desire to
> > change the C++ language much; I like it just how it is, but I think an
> > optional warning would be useful during development under g++.
> 
> Any such option cannot use the word "must", because it implies a language
> requirement that is not present.

Ok, fair enough, how about "This option helps detect unhandled exceptions by
reporting violations of an extra-language constraint that all throwable
exceptions should be declared in the prototype".

And a warning like

"Warning: method can throw an exception undeclared in it's prototype, and
you said you didn't want this to be allowed."

> What you're seeing below is standard C++ behavior.  If a function
> declares that it will only throw int, but it throws something else,
> it immediately terminates.  That's the way C++ assures that promises
> like throw(int) are kept.

Someone pointed this out already. That was a misunderstanding of mine.
(I actually expected gcc to try to convert the const char* to int)

---
Wes

^ permalink raw reply	[flat|nested] 22+ messages in thread
* Re: Warnings for unhandled c++ exceptions?
@ 2003-02-03 15:24 Bonzini
  2003-02-03 16:45 ` Wesley W. Terpstra
  0 siblings, 1 reply; 22+ messages in thread
From: Bonzini @ 2003-02-03 15:24 UTC (permalink / raw)
  To: gcc; +Cc: terpstra

> int foo() throw (int)
> {
>     throw "this will abort -- why? I broke my promise, but no warning...";
> }
> ...
> catch (const char *s)

A warning might be desirable, but it is not an odd behavior, as the purpose
of the throw clause (aka exception specification) is exactly to abort if an
exception that's not mentioned there propagates above it.

"Whenever an exception is thrown and the search for a handler (15.3)
encounters the outermost block of a function with an
exception-specification, the function unexpected() is called (15.5.2) if the
exception-specification does not allow the exception. (15.4.8)."

"exception handling must be abandoned [...] when unexpected() throws an
exception which is not allowed by the previously violated
exception-specification, and std::bad_exception is not included in that
exception-specification." (15.5.1.1) "In such cases, void terminate(); is
called." (15.5.1.2)

Anyway I don't think the warning should be enabled by default as wide bodies
of C++ code don't use exception specifications.

Paolo


^ permalink raw reply	[flat|nested] 22+ messages in thread
* Warnings for unhandled c++ exceptions?
@ 2003-02-03 13:13 Wesley W. Terpstra
  2003-02-03 17:07 ` Joe Buck
  2003-02-03 19:13 ` Mark Mielke
  0 siblings, 2 replies; 22+ messages in thread
From: Wesley W. Terpstra @ 2003-02-03 13:13 UTC (permalink / raw)
  To: gcc

Is there a compiler flag for g++ which would warn on these code snippets:

1.

void foo()
{
	throw 4;
}

Warning: Exception 'int' must be caught, or it must be declared in throw ()
clause of this method.

2.

void foo() throw (int)
{
	throw 4;
}

void bar()
{
	foo();
}

Warning: ... bar ... 'int' must be caught or ...

----------------------------

I find this feature of Java remarkably useful as it helps to detect sloppy
exception handling, which is brutal to find in C++. I do not desire to
change the C++ language much; I like it just how it is, but I think an
optional warning would be useful during development under g++.

Since g++ can also invoke Java methods, and in turn be called by them, it
also makes sense that g++ should be able to maintain the Java-style
guarantees about only throwing what one declares throwable.

Is there some undocumented -W option to turn this warning on?
Is there some patch someone has I can apply to add this feature?
And if not ... shouldn't there be?

----------------------------

Relatedly, I'm sure you are aware of this odd behaviour:
	(debian gcc 3.2.2-0pre5)

int foo() throw (int)
{
	throw "this will abort -- why? I broke my promise, but no warning...";
}

int main()
{
	try
	{
		foo();
	}
	catch (const char* s)
	{
		// should catch here...
	}
	catch (int x)
	{
		// in case of a weird conversion
	}
	return 0;
}

----------------------------

Thanks for your time.

---
Wes

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2003-02-03 21:37 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-03 21:37 Warnings for unhandled c++ exceptions? Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
2003-02-03 21:25 Robert Dewar
2003-02-03 19:39 Chris Lattner
2003-02-03 19:47 ` Wesley W. Terpstra
2003-02-03 20:12   ` Gabriel Dos Reis
2003-02-03 19:16 Wesley W. Terpstra
2003-02-03 19:14 Wesley W. Terpstra
2003-02-03 15:24 Bonzini
2003-02-03 16:45 ` Wesley W. Terpstra
2003-02-03 18:13   ` Alexandre Oliva
2003-02-03 18:17     ` Gabriel Dos Reis
2003-02-03 13:13 Wesley W. Terpstra
2003-02-03 17:07 ` Joe Buck
2003-02-03 19:13 ` Mark Mielke
2003-02-03 19:24   ` Wesley W. Terpstra
2003-02-03 19:31     ` Gabriel Dos Reis
2003-02-03 19:37       ` Wesley W. Terpstra
2003-02-03 19:45         ` Phil Edwards
2003-02-03 20:08           ` Gabriel Dos Reis
2003-02-03 20:14             ` Phil Edwards
2003-02-03 20:15               ` Gabriel Dos Reis
2003-02-03 20:45                 ` Andreas Schwab

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).