This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Warnings for unhandled c++ exceptions?
- From: "Wesley W. Terpstra" <terpstra at ito dot tu-darmstadt dot de>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 3 Feb 2003 14:13:45 +0100
- Subject: Warnings for unhandled c++ exceptions?
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