This is the mail archive of the gcc-bugs@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]

g++: suggestion for warning dealing with exception handling



This isn't really a bug report -- it's a suggestion for a warning that
would be useful and probably easy to implement.  First:

   % g++ -v
   Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/specs
   gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)

This is part of a RedHat 6.0 GNU/Linux system.

Consider the following code fragment:

     #include <iostream.h>

     class Base
     {
     };

     class Derived: public Base
     {
     };


     int main(int, char*[])
     {
	 try
	 {
	     throw Derived();
	 }
	 catch (Base)
	 {
	     cout << "caught base" << endl;
	 }
	 catch (Derived)
	 {
	     cout << "caught derived" << endl;
	 }
	 return 0;
     }

This code is almost certainly erroneous.  If you run it, it says
"caught base".  The reason is that catch clauses are evaluated in
order of appearance, and an instance of "Derived" matches both Base
and Derived.  It would be nice if gcc would warn when a catch clause
will never be reached because another catch clause that precedes it
encompasses it.

--
E. Jay Berkenbilt (ejb@ql.org)  |  http://www.ql.org/q/


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