Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:57:24+00:00 2026-06-07T05:57:24+00:00

Looking at some old code we have lots of things like the following: //

  • 0

Looking at some old code we have lots of things like the following:

// This is dumb
string do_something(int in)
{
    stringstream out;
    try
    {
        out << std::fixed << in;
    }
    catch(std::exception &e)
    {
        out << e.what();
    }

    return out.str();
}

// Can't we just do this? Can this ever fail?
string do_something_better(int in)
{
    stringstream out;
    out << std::fixed << in;
    return out.str();
}

When a stringstream reads a primitive can it ever throw an exception? What about when reading a string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:57:26+00:00Added an answer on June 7, 2026 at 5:57 am

    Summarizing a few answers

    By default, streams don’t throw exceptions. They can if they are enabled.

    stringstream out;
    out.exceptions(std::ios::failbit);   // throw exception if failbit gets set
    

    According to the
    Apache C++ Standard Library User’s Guide

    The flag std::ios_base::badbit indicates problems with the underlying stream buffer. These problems could be:

    Memory shortage. There is no memory available to create the buffer, or the buffer has size 0 for other reasons (such as being provided from outside the stream), or the stream cannot allocate memory for its own internal data, as with std::ios_base::iword() and std::ios_base::pword().

    The underlying stream buffer throws an exception. The stream buffer might lose its integrity, as in memory shortage, or code conversion failure, or an unrecoverable read error from the external device. The stream buffer can indicate this loss of integrity by throwing an exception, which is caught by the stream and results in setting the badbit in the stream’s state.

    Generally, you should keep in mind that badbit indicates an error situation that is likely to be unrecoverable, whereas failbit indicates a situation that might allow you to retry the failed operation.

    So it seems like the safest way to do this would be

    string do_something(int in)
    {
        stringstream out; // This could throw a bad_alloc
        out << std::fixed << in; // This could set bad or fail bits
    
        if(out.good())
        {
            return out.str();
        }
        else
        {
            return "";
        }
    }
    

    This is overkill though because according to Handling bad_alloc if creating the stream fails, there are bigger problems to worry about, and the program is probably going to exit. So assuming it gets past creating the stream, it’s possible but extremely unlikely that the badbit gets set. (Stream gets allocated with memory < sizeof(int)).

    It’s also unlikely that the failbit gets set (not sure of a use case for reading off the stack other than a corrupt stack). So the following code is sufficient, as recovering from a stream error at this point is unlikley.

    string do_something(int in)
    {
        stringstream out;
        out << std::fixed << in;
        return out.str();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some old school looking code that is as follows: IList<KeyValuePair<string, ValuePair>> ServicePairs
While looking through some old code I came across this gem: MyObject o =
I have been looking into refactoring some old code into a new WCF service,
Looking at some assembly code for x86_64 on my Mac, I see the following
I have some old C# plugin code that was implemented strictly with Reflection. In
Ahoy! I've been looking into updating some old test code in an attempt to
I have an application that is looking through some files for old data. In
I have been looking at Prism to host an old Winforms application. This is
I am looking at some old code in Perl, where the author has writtern
Looking at some old code in Java 1.4 but I'm unfamiliar with Java. Does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.