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]
Other format: [Raw text]

[Bug c++/11238] New: A constructor with exception handler rethrows the exception to the constructor caller


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11238

           Summary: A constructor with exception handler rethrows the
                    exception to the constructor caller
           Product: gcc
           Version: 2.96 (redhat)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: ejyanezp@yahoo.com
                CC: gcc-bugs@gcc.gnu.org

*A constructor with exception handler rethrows the 
* exception to the constructor caller

Hi g++ guys, 

First of all: great job!. Ok, now to the point:

I'm studing c++ and there is something that, in my opinion, 
executes abnormally when using a constructor initializer list 
inside a try-catch block. A sample code is below.

I did not use any include files to make it easier the bug 
report. The little program returns to the OS the value of the 
globalResult variable.

The constructor of the class CTinyPair has an exception handling 
try-catch block. So, any exception should be handled there and should 
not be handled again outside the constructor. But the behavior is like
when the catch block in the constructor makes a 'throw;' statement
to rethrow the exception, propagating it to the main function.

You can realize this by evaluating the '$?' Shell variable after the
program execution on a unix-like system (in my case is RedHat linux):

# echo $?
3
#

The resulting value of globalResult variable should be 1.

I hope the description is usefull and aid to you to improve this compiler.
I think this is a bug, not a feature. If its a feature, i´ll appreciate some
URL/document where i can read the explanation.

Thanks!.

Best regards,
Eduardo Yanez.

******************************************

int globalResult(0);

const int EXC_HANDLED_IN_CONSTRUCTOR=1;
const int EXC_HANDLED_IN_MAIN=2;

struct InitError {
        InitError(int value) : m_value(value) {}
        int m_value;
};

class CTinyInteger {
public:
        CTinyInteger(int value) throw (InitError) {
            if (value < 0 || value > 32) throw InitError(value);
                m_value=value;
        }
        int getValue() { return m_value; }
private:
        short m_value;
};

class CTinyPair {
public:
        CTinyPair(int a, int b) throw (InitError)
        try : m_a(a), m_b(b) {}
        catch (const InitError& exc) {
           globalResult+=EXC_HANDLED_IN_CONSTRUCTOR; }
private:
        CTinyInteger m_a, m_b;
};

int main(int argc, char *argv[]) {
        try { CTinyPair p(5, -5); }
        catch (const InitError& exc) { globalResult+=EXC_HANDLED_IN_MAIN; }
        return globalResult;
}


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