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

Re: Throwing std::ios_base::failure on formatted input with gcc 6.2


On 10/26/2016 7:51 AM, Jonathan Wakely wrote:
On 26 October 2016 at 12:48, Jonathan Wakely wrote:
On 26 October 2016 at 12:45, Edward Diener wrote:
If I have code asuch as:

std::stringstream ss;
ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
char c;
ss >> c;

I would except an exception of std::ios_base::failure to be thrown yet gcc
6.2 is throwing some other exception. Is there a reason for this ?

There are two versions of std::ios::failure, one using the old ABI
(called std::ios_base::failure) and one using the new ABI (called
std::ios_base::[abi:__cxx11]failure).

You're trying to catch the new one, but the library throws the old
one. I'm probably going to change the library to throw the new one for
GCC 7.


I forgot to say, the reason for the two versions is that C++11
required an ABI break for that type, adding a new std::system_error
base class, with extra data members. That base class wasn't present in
C++98 (because the type didn't exist) and so we've been shipping a
library for years that throws the old type.

They both have a base class of std::runtime_error so you can still
catch it as that, or std::exception.


The program:

#include <iostream>
#include <sstream>
int main()
    { 	
    std::stringstream ss;
    ss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
    char c;

    try
        {
        ss >> c;
        std::cout << "Exception not thrown.";
        }
    catch (std::runtime_error &)
        {
        std::cout << "Exception std::runtime_error thrown.";
        }
    catch (...)
        {
        std::cout << "Unknown exception thrown.";
        }
    }

when built with gcc-6.2 outputs:

'Unknown exception thrown.'

According to what you have written above I would expect the output to be

"Exception std::runtime_error thrown."

What am I missing here ?



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