This code prints (erroneously) Hello, world! 1 in g++ 4.8.1, g++ 4.6.3, but works correctly in g++ 4.5.2. cat main.cpp #include <string> #include <iostream> struct Exception { std::string what; Exception(const std::string & what_) : what(what_) {} }; std::string f() { throw Exception("Hello, world!"); } void g() { try { f(); } catch (...) { throw Exception(f()); } } int main(int argc, char ** argv) { try { g(); } catch (const Exception & e) { std::cerr << e.what << std::endl; } /// returns 1 in gcc 4.6.3, 4.8.1 - bug std::cerr << std::uncaught_exception() << std::endl; return 0; } # g++-4.8 --version g++-4.8 (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1 Copyright (C) 2013 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # g++-4.8 main.cpp # ./a.out Hello, world! 1 # g++-4.6 --version g++-4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # g++-4.6 main.cpp # ./a.out Hello, world! 1 # g++-4.5 --version g++-4.5 (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # g++-4.5 main.cpp # ./a.out Hello, world! 0 http://ideone.com/lx2ao2
Confirmed. uncaught_exception becomes true while evaluating the operand of the throw statement, but should only be true after initialization of the exception object is complete. #include <exception> int f() { if (std::uncaught_exception()) std::terminate(); return 1; } int main() { try { throw f(); } catch (...) { } }
Possibly a dup of PR 41174 (although that was wrong prior to 4.6.0)
Author: jason Date: Mon Jan 27 13:57:39 2014 New Revision: 207129 URL: http://gcc.gnu.org/viewcvs?rev=207129&root=gcc&view=rev Log: Core DR 475 PR c++/41174 PR c++/59224 * libsupc++/eh_throw.cc (__cxa_throw): Set uncaughtExceptions. * libsupc++/eh_alloc.cc (__cxa_allocate_dependent_exception) (__cxa_allocate_exception): Don't set it here. Added: trunk/gcc/testsuite/g++.dg/eh/uncaught4.C Modified: trunk/gcc/testsuite/g++.dg/eh/uncaught1.C trunk/libstdc++-v3/ChangeLog trunk/libstdc++-v3/libsupc++/eh_alloc.cc trunk/libstdc++-v3/libsupc++/eh_throw.cc
Author: jason Date: Mon Jan 27 13:58:48 2014 New Revision: 207131 URL: http://gcc.gnu.org/viewcvs?rev=207131&root=gcc&view=rev Log: Core DR 475 PR c++/41174 PR c++/59224 * libsupc++/eh_throw.cc (__cxa_throw): Set uncaughtExceptions. * libsupc++/eh_alloc.cc (__cxa_allocate_dependent_exception) (__cxa_allocate_exception): Don't set it here. Added: branches/gcc-4_8-branch/gcc/testsuite/g++.dg/eh/uncaught4.C Modified: branches/gcc-4_8-branch/gcc/testsuite/g++.dg/eh/uncaught1.C branches/gcc-4_8-branch/libstdc++-v3/ChangeLog branches/gcc-4_8-branch/libstdc++-v3/libsupc++/eh_alloc.cc branches/gcc-4_8-branch/libstdc++-v3/libsupc++/eh_throw.cc
Fixed for 4.8.3/4.9. Not fixing on 4.7 branch.
Author: jason Date: Tue Apr 1 17:28:29 2014 New Revision: 208991 URL: http://gcc.gnu.org/viewcvs?rev=208991&root=gcc&view=rev Log: Core DR 475 PR c++/41174 PR c++/59224 * libsupc++/eh_throw.cc (__cxa_throw): Set uncaughtExceptions. * libsupc++/eh_alloc.cc (__cxa_allocate_dependent_exception) (__cxa_allocate_exception): Don't set it here. Added: branches/gcc-4_7-branch/gcc/testsuite/g++.dg/eh/uncaught4.C Modified: branches/gcc-4_7-branch/gcc/testsuite/g++.dg/eh/uncaught1.C branches/gcc-4_7-branch/libstdc++-v3/ChangeLog branches/gcc-4_7-branch/libstdc++-v3/libsupc++/eh_alloc.cc branches/gcc-4_7-branch/libstdc++-v3/libsupc++/eh_throw.cc
OK, fixed for 4.7.4 as well.
(In reply to Jason Merrill from comment #5) > Fixed for 4.8.3/4.9. Not fixing on 4.7 branch. It will lead to the lsb test caes /libstdcxx-t2c/tests/LanguageSupport/LanguageSupport FAIL. So I want to know the right return value of std::uncaught_exception() (inside exception-constructor). The LSB test code is as follows: cat main.cpp #include <string.h> #include <iostream> using namespace std; class exception_test : public exception { public: bool result; exception_test(): exception(){result = uncaught_exception();} ~exception_test() throw(){}; }; int main(int argc, char ** argv) { bool result; try { throw exception_test(); } catch(exception_test et) { result = et.result; } cerr << result << endl; return 0; } # g++ ./main.cpp # ./a.out 0 My GCC version is 4.9.2, the return value of result is 0,and the test is FAIL. If the the return value of result is 1,and the test will be PASS.
(In reply to li xin from comment #8) > It will lead to the lsb test caes > /libstdcxx-t2c/tests/LanguageSupport/LanguageSupport FAIL. > So I want to know the right return value of std::uncaught_exception() > (inside exception-constructor). What isn't clear about the bug report? The right value is the one G++ gives now. The LSB test is wrong.