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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:26:44+00:00 2026-05-26T16:26:44+00:00

I have several questions about best use of exceptions in Java. Consider the code

  • 0

I have several questions about best use of exceptions in Java.

Consider the code below:

private String doHttpGetRequest(String url) throws IOException {
...
}

private Photo processJson(String json) throws JSON Exception{
...
}

private Photo getPhoto() throws IOException, JSON Exception {
    String url = "http://...";
    String response = doHttpGetRequest(url);
    Photo photo = processJson(response);
    photo.downloadImage();
    return photo;
}

public static void main(String args[]) {
    Photo p = null; 
    try {
        p = getPhoto();
    } catch( JSONException j ) {
        // Re-try a few times, then display user alert
    } catch( IOException e ) {
        // Re-try a few times, then display user alert
    }

    if( p!=null)
        // now display photo
}

This code is for an android app where the connection may not be very reliable, so I would like to re-try the getPhoto() method a few times to see if it will work, then fail and alert the user.

My questions:

  1. I am opening an InputStream in doHttpGetRequest() and that method is throwing an IOException. How do I close the InputStream if it has thrown the exception? Without a finally block within the method that would allow me to close resources, i’m confused.

  2. In main(), I’m only interested in whether getPhoto() works or does not work. Would I be better to surround statements inside getPhoto() with a try/catch block and catch the JSONException, throwing a new IOException when it is caught? This would lead to only having to catch one kind of exception in main(), leading to simpler code and not having duplicate functionality.

  3. If p.getPhoto() throws an exception, I want to re-try it (possibly twice) and then display a user alert if this fails. Is there a common programming structure to doing this?

  • 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-26T16:26:45+00:00Added an answer on May 26, 2026 at 4:26 pm
    1. doHttpGetRequest() should be in charge of closing the inputstream. Finally-block there would be suitable for closing (why don’t you want finally block?).

    2. Do you have to pass any exceptions to the main method. You could surround doHttpGetRequest(url) with try{} and surround processJson with another (I quess this one doesn’t benefit from retries). This would make the main() method cleaner.

    3. If you go with my suggestion on 2. you could do the retrying like this:

      String response = null;
      for (int i = 0; i < RETRY_COUNT && response == null; i++){
        try {
          response = doHttpGetRequest(url);
        } catch (IOException e){
          // Possibly log the error here
        }
      }
      if (response  == null) return null;
      

    EDIT: Bugfix

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

Sidebar

Related Questions

There have been several questions over the past few days about the proper use
I have several questions regarding code contracts, and the best practices for their usage.
There have been several questions already posted with specific questions about dependency injection ,
There have been several questions recently about database indexing and clustered indexing and it
Several questions about functional programming languages have got me thinking about whether XSLT is
I thought about migrating subversion a lot and I have several questions still open.
There have been several questions posted to SO about floating-point representation. For example, the
I have seen several questions with people asking about the same problem but none
I have several questions about the PE subsystem field, which may overlap to some
I have lately read several articles and questions about Model Binding and Validation in

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.