Warnings for unhandled c++ exceptions?

Wesley W. Terpstra terpstra@ito.tu-darmstadt.de
Mon Feb 3 19:14:00 GMT 2003


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



More information about the Gcc mailing list