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

Edward Diener eldlistmailingz@tropicsoft.com
Thu Oct 27 13:50:00 GMT 2016


On 10/27/2016 6:35 AM, Jonathan Wakely wrote:
> On 27 October 2016 at 04:44, Edward Diener wrote:
>> On 10/26/2016 7:51 AM, Jonathan Wakely wrote:
>>> 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 ?
>
> That I was wrong to say both types have a std::runtime_error base.
> C++98 says std::ios_base::failure derives directly from
> std::exception.
>

Even when I add -std=c++11 to the command line, the same result occurs.

I can even add:

     catch (std::ios_base::failure &)
         {
         std::cout << "Exception std::ios_base::failure thrown.";
         }

before:

     catch (std::runtime_error &)
         {
         std::cout << "Exception std::runtime_error thrown.";
         }

in my example above, and compile with -std=c++11, and the output is 
still "Unknown exception thrown."



More information about the Gcc-help mailing list