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]

enh rqst: warning about throw lists



I have an enhancement request for g++ to do with throw lists.
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 gives a warning. In my
opinion this would make throw lists more useful like they are in Java.

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 (via
unexpected->terminate).
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.

What do you think guys? I think that without a warning about possible
throw list violation, throw lists are next to useless. Especially when the
penalty for making a mistake is death. Maybe this is why ANSI themselves
do not use throw lists in standard library code that is defined to throw
exceptions under certain conditions.

Regards, 

Andrew Marlow.



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