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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:04:03+00:00 2026-05-26T14:04:03+00:00

I sometimes wonder how to handle construction of objects which can throw in their

  • 0

I sometimes wonder how to handle construction of objects which can throw in their constructor. I wonder how you do it.

Consider the following snippet. I have a class, named TCPMessage, which represents a message my “server” receives over TCP. If the received message is invalid (i.e. the CRC32 calculated in the TCPMessage‘s constructor doesn’t check out), TCPMessage‘s constructor throws.

So below is how I do it. Do you know of any better way? I’m asking, because it doesn’t really look too elegant.

void TCPConnection::handleRead(
  const boost::system::error_code& error,
  char* read_buffer
)
{
  if (!error) {
    TCPMessage* message = NULL; // being verbose here
    try {
      message = new TCPMessage(read_buffer);
    } catch (const char* e) {
      std::cerr << "Instatiating TCPMessage: " << e << std::endl;
    } catch (...) {
      std::cerr << "Instatiating TCPMessage: unknown exception." << std::endl;
    }

    if (message) {
      // if created succesfully
      // process the message and delete it
      SeekurJrRC::Core::Driver& driver = boost::asio::use_service<SeekurJrRC::Core::Driver>(_io_service);
      driver.processMessage(*message);
      delete message;
    }
  }
  delete [] read_buffer;
}

Oh yeah, an I know about knowing better than to use char* read_buffer and deleting it in another function. shared_ptr‘s the way, I know.

  • 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-05-26T14:04:04+00:00Added an answer on May 26, 2026 at 2:04 pm

    Your problem isn’t exceptions, it’s raw pointers and lack of RAII.

    Sanitizing your code a bit:

    void TCPConnection::handleRead(
      const boost::system::error_code& error,
      char* read_buffer
    )
    {
      if (!error) {
        try {
          TCPMessage message(read_buffer);
          SeekurJrRC::Core::Driver& driver = boost::asio::use_service<SeekurJrRC::Core::Driver>(_io_service);
          driver.processMessage(message);
    
        } catch (const char* e) {
          std::cerr << "Instatiating TCPMessage: " << e << std::endl;
        } catch (...) {
          std::cerr << "Instatiating TCPMessage: unknown exception." << std::endl;
        }
      }
    }
    

    new calls should be wrapped in RAII objects, not dangle around freely in your user code. delete calls should never be explicit, but instead be handled by the destructors defined in your RAII objects.

    Then your objects will automatically get destroyed and clean up after themselves if an exception is thrown, and you don’t need your over-complicated “try/catch/check-for-success” dance. If an exception was not thrown, you continue normally. If it was thrown, you leave the try block, and your objects are destroyed automatically.

    Note that here you don’t actually need the try/catch block any more. The only thing you use the catch for is to print an error message. It’s not necessary for the program flow or to prevent resource leaks. Normally you would handle the error where you can meaningfully do so. Presumably that’s somewhat higher up the call tree, where you know what to do about a failed read. At this level, it makes more sense to just let the exception escape to indicate that an error occurred.

    void TCPConnection::handleRead(
      const boost::system::error_code& error,
      char* read_buffer
    )
    {
      if (!error) {
        TCPMessage message(read_buffer);
        SeekurJrRC::Core::Driver& driver = boost::asio::use_service<SeekurJrRC::Core::Driver>(_io_service);
        driver.processMessage(message);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using debug() more often now, but sometimes I wonder which functions have
Sometimes I wonder that we do have interfaces and abstract classes for two different
Sometimes statistics Android Market makes me wonder. When app have total installs ~10000-12000 it
Sometimes I really wonder if my code is lastable. I do everything to make
Sometimes when I see my logging code I wonder if I am doing it
Sometimes I have to work on code that moves the computer clock forward. In
Sometimes a labeled break or continue can make code a lot more readable. OUTERLOOP:
I wonder why div elements sometimes are zerosized while containing html layout. I want
Sometimes you can see: do_this do available_method1 arg1 available_method2 arg1 end When I use
Sometimes when I browse views or packages in Oracle SQL Developer they have a

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.