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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:27:12+00:00 2026-05-25T01:27:12+00:00

I would like to have the following method which returns HttpResponse . But at

  • 0

I would like to have the following method which returns HttpResponse. But at the same time I do not want to initialize HttpResponse to null.
Which is a good way to do, adding throws for the method or adding try/catch blocks with HttpResponse initialized to null.
HttpResponse is an interface so initializing this doesn’t make any sense.
I am thiking of adding getters/setters for HttpResponse declaring this as a member variable.

Ideas are appreciated.

public HttpResponse executeRestClientServiceCall(final HttpUriRequest request)
    {
        // Verifier;
        HttpResponse httpResponse = null;
        final HttpClient client = new DefaultHttpClient();
        try
        {
            httpResponse = client.execute(request);
            return httpResponse;
        }
        catch (final ClientProtocolException e)
        {
        }
        catch (final IOException e)
        {
        }
        return httpResponse;
    }
  • 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-25T01:27:13+00:00Added an answer on May 25, 2026 at 1:27 am

    You should not catch the exception at this point. Not at all. This makes your code much simpler. What you have to do though is to release all the resources that you used, and do so properly. This is the code to do that:

    @Nonnull
    public HttpResponse executeRestClientServiceCall(HttpUriRequest request) throws IOException {
      HttpClient client = new DefaultHttpClient();
      HttpResponse httpResponse = client.execute(request);
      try {
        // ... other code ...
      } finally {
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
          entity.getContent().close();
        }
      }
      return httpResponse;
    }
    

    At this point you cannot decide what to do with the exception, so the best choice is to just let it slip through to the caller. Maybe he knows what to do with it.

    Throwing an exception gives the caller more possibilities than just returning null.

    Considering your other questions:

    But at the same time I do not want to initialize HttpResponse to null.

    This is good, since you can write your code so that the variable always holds a proper object. See the code above.

    Which is good way to do adding throws for the method or adding try/catch blocks with httpResponse initialized to null.

    I answered it above. The good way is to add a throws clause for the method.

    I am thiking of adding getters/setters for HttpResponse declaring this as a member variable.

    Don’t do that. The HttpResponse is an object that typically has a very short life span. It should always be stored in a local variable, and when the method returns, there is no need to keep it. This is different from the HttpClient, which you should store in a field of a class, so you can add a connection pool later if you need it.

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

Sidebar

Related Questions

With VMWare Server running under Linux (Debain), I would like to have the following
I have the following string and I would like to remove <bpt *>*</bpt> and
I have the following snippet where I would like to extract code between the
I have the following 2 arrays and would like to combine them. I'm more
I have the following issue and I would like to know what exactly happens.
I have the following LINQ that I would like to also order by file
Assume I have the following input in Pig: some And I would like to
I have the following three tables: alt text http://img16.imageshack.us/img16/5499/linqtosqlquery.jpg With LinqToSql I would like
I have the following construction: for (String playerName: players) . I would like to
I have the following HTML: <div id=mydiv> </div> I would like to load content

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.