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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:01:05+00:00 2026-05-28T04:01:05+00:00

I am trying to return a String from the following method. public String openCon()

  • 0

I am trying to return a String from the following method.

public String openCon() {
    try {
        Scanner scan = new Scanner(System.in);
        URL sitex = new URL("http://" + scan.nextLine());
        URLConnection connection = sitex.openConnection();
        Object content = sitex.getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(sitex.openStream()));
        String str;
        String x = "1";
        while ((str = in.readLine()) != null) {
          x += str;
        }
        in.close();
        return x;

   }
   catch(Exception e) {
        System.out.println(e);
   }
}
  • 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-28T04:01:05+00:00Added an answer on May 28, 2026 at 4:01 am

    The problem isn’t returning from the try block – the problem is that you aren’t returning anything if an exception is thrown. You’re catching the exception… but then reaching the end of the method without returning anything. (To put it in more technical terminology: the end of a non-void method should not be reachable.)

    Personally, I would just remove the catch block entirely, and add throws declarations for any exceptions which are thrown within the body. You’re not really handling the exceptions – you’re just printing them out and ignoring them, which is very rarely a good idea. Catching Exception is usually a pretty bad idea to start with.

    As an aside, you should also close your BufferedReader and URLConnection values in a finally block so they’re closed even in the case of an exception. I’d also suggest either passing a fixed encoding name to InputStreamReader, or using a higher-level HTTP client API which will use the content-type header from the response. Oh, and use StringBuilder instead of string concatenation in a loop.

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

Sidebar

Related Questions

I have the following method: public IEnumerable<Foo> GetFoo(int x, string y) { return from
im trying to return a string value from a method inside my script tag
I am trying to use the following method: public static String UrlToString(String targetURL, List
I'm trying to translate the following slugify method from PHP to C#: http://snipplr.com/view/22741/slugify-a-string-in-php/ Edit:
I'm trying to return a string from a function. Which basically adds some chars
Hey, I am trying to return a user defined class from a web method.
I am basically trying to return from a method which reads user input from
I'm trying to create a method in C# to return a string of a
I am trying to run the following C++ method from Java using JNA, but
I used the following method to create bits from a string, but now I

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.