This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Warnings for unhandled c++ exceptions?


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 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.  
 
> Relatedly, I'm sure you are aware of this odd behaviour:
> 	(debian gcc 3.2.2-0pre5)

You should be aware of this odd behavior, too, if you want to pass yourself
off as a C++ programmer. :-)

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.
 
> 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

-- 
Q. What's more of a headache than a bug in a compiler.
A. Bugs in six compilers.  -- Mark Johnson


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]