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 7849819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:39:05+00:00 2026-06-02T18:39:05+00:00

I’m a newbie at C++ (coming from C). I understand conceptually how RAII should

  • 0

I’m a newbie at C++ (coming from C). I understand conceptually how RAII should work, but I have trouble fitting a simple socket connection handler into it.

Current code:

void accept_ev(event_handler::token &t, int listenfd)
{
    int newfd = accept(listenfd, NULL, NULL);

    if (newfd < 0)
        throw api_server_accept_failed(*this, errno);

    connections.insert(api_server_connection(newfd));
}

This is obviously not safe, since api_server_connection constructor might raise an exception before assigning the fd to its member variable.

So my next thought was to move the accept into the constructor. The problem is I really want api_server_connection to be agnostic about where the fd came from. E.g. if I want to support inetd in the future it might just as well be passed into the program as fd 0.

So how do I do this. Should I use different constructors for each way to obtain an fd? Should I perhaps make subclasses? Another option could be to have a lambda function?

Or should I just catch any error and close the fd in the caller in that case?

  • 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-02T18:39:06+00:00Added an answer on June 2, 2026 at 6:39 pm

    Ignoring, sockets for the moment, what you usually want to do is divide things into two phases.

    In the first phase, you do things that may throw, but if they do, you can restore the system to a sane state (preferably a state as if nothing happened at all).

    In the second phase, you do things that you may not be able to undo, but that you know for sure will never throw.

    To do that, you need some assurance about what can/will throw and (particularly) some assurances about at least a few rather specific operations that can never throw at all (e.g., swapping two items).

    To facilitate this, you normally want to do that restoration to a sane state in the dtor, so if an exception is thrown, the destructor will clean up automatically.

    Unfortunately, it’s difficult to say a lot more than that about your specific code/situation, because we simply don’t know enough about the classes you’re using.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.