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]

GNU C++ compiler enhancement rqst



I have an enhancement request for g++ to do with exceptions.
This enhancement is not to be default behaviour. It would
have to be enabled by some command line option.

When a function has a throw list (aka exception specification)
the compiler should check that the throw list is capable of
being honoured. If it might be violated then this is a
fatal compilation error.

Here's a example of code that has a throw list that cannot
be honoured (class E is an exception class):

class A
{
public:
    void doSomething()
    {
        // .....insert some code here
    };

    void doSomethingElse() throw(E)
    {
        doSomething();
    }
};

The above code is wrong because doSomethingElse() makes a
promise it might not be able to keep. And as we know, in
C++ the penalty for such a broken promise is death.
We have said that doSomethingElse will only throw exceptions
of type E but it calls a routine that might throw any
exception, which will be propaged, violating the throw list.

Here is the code corrected:

class A
{
public:
    void doSomething()
    {
        // .....insert some code here
    };

    void doSomethingElse() throw(E)
    {
        try
        {
            doSomething();
        }
        catch(...)
        {
        }
    }
};

Now any exception is caught and suppressed. The throw list
is honoured.

Why do I want this enhancement? Because without it I believe
throw lists are next to useless. In fact it could be argued
that throw lists are actually harmful. They start out as
a potentially useful documentation aid but inadvertant
violation, not being checked by the compiler, can result
in a terminated program.

Have you noticed that the ANSI standard clearly says which 
bits of the STL throw exceptions and that those methods
do not throw any other kind of exception, yet ANSI do not
use throw lists? There is occasional use of throw lists
(e.g in some destructors) but it is used infrequently
and inconsistently. Throw lists tend to have a cascading
effect. Once you start to use them the compiler tends to
check for consistency but these checks are nearly always
incomplete as the previous example shows.

I realise that this enhancement is non-trivial, especially
in the presence of template code (where some coding
standards specifically ban throw lists). However, if at
least ONE compiler supported full throw list checking then
I would like to see ANSI change the standard and REQUIRE it
of all compilers. Then the STL could make official use of
throw lists and so could applications without this causing
run-time failures that we have at present.

One of the philosophies of C++ is to prefer compile-time
errors to run-time errors. This enhancement is in keeping
with this ideal.

Regards,

Andrew Marlow.





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