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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:23:14+00:00 2026-06-09T09:23:14+00:00

I have a piece of network code that throws various exception which are all

  • 0

I have a piece of network code that throws various exception which are all caught under a general catch exception statement.

try {
    network code
} catch (Exception e) {
    freeaddrinfo(some_address);
}

The problem with this method is the freeaddrinfo within the exception. Not all cases of exception can/should call freeaddrinfo and in my specific case freeaddrinfo should not be called when there is an invalid address that is passed into the network code to connect somewhere. My idea of solving this problem is to retrieve the error code that was thrown by this exception and to possibly use this in combination with e.to_string to deal with this edge case. From what I have learned from Effective Java, this is a fragile way of dealing with this problem. What do you recommend I should do?

  • 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-09T09:23:15+00:00Added an answer on June 9, 2026 at 9:23 am

    In C++, the usual way to release a resource after an exception is not to catch the exception, but to wrap the resource in an object whose destructor is responsible for releasing. That way, it is released when the management object goes out of scope (or is otherwise destroyed), whether or not an exception is thrown. This technique is known as RAII.

    A (very basic) management object might look like:

    struct AddressInfo {
        addrinfo * info;
    
        // Constructor takes ownership of resource
        explicit AddressInfo(addrinfo * info) : info(info) {}
    
        // Destructor releases resource
        ~AddressInfo() {freeaddrinfo(info);}
    
        // Prevent shallow copying, so only one object manages the resource
        AddressInfo(AddressInfo const &) = delete;
        void operator=(AddressInfo const &) = delete;
    };
    

    Then your code would become:

    // some network code
    AddressInfo address(some_address);
    // some more network code
    

    with no need for any exception handling or manual clean-up code.

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

Sidebar

Related Questions

I have a piece of code that was written by someone else before the
I have a piece of Python code that is supposed to open a new
I have a piece of code (below) that can get the text of an
I have a piece of code that invokes an instance of the bash terminal
I have a simple piece of code in a .NET console application that tries
I have a piece of C network software that currently works in listen and
I have this piece of code that drives me mad. FUNCTION DiskInDrive(CONST DriveNumber: Byte):
I have a problem with a piece of legacy c++/winsock code that is part
I have piece of code in C which removes any duplicate character in a
I have a piece of code in c# that checks, if a value is

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.