Results 1 to 6 of 6

Thread: Try/catch block

  1. #1
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Question Try/catch block

    public class UseTry
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

    String getName()
    {
    try
    {
    return br.readLine();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    }
    }
    It gives an error "Method must return a result of type string" ..There is a solution if i explicitly throw Exception Like this

    String getName() throws IOException
    {
    return br.readLine();
    }

    No error here..but the problem is that my program requires i should not explicitly throw the exception...

    Can u all suggest sumthing to rectify this problem..
    Thanks :)

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you may declare and initialize varialbe xxx
    and xxx = br.realine()
    return xxx at the end of method

    OR

    return xxx in finally block

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The error is you've not return anything from the complete path. Say in try block anything is not return due to any reason. So outside of the try-catch block you've not return anything too. But your method expecting to return something, and failed. Those things are validate in compile time.

    Java Code:
        String getName() {
    
            try {
                return br.readLine();
            }
            catch (IOException e){
                e.printStackTrace();
            }
            return "";
        }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mtyoung View Post
    you may declare and initialize varialbe xxx
    and xxx = br.realine()
    return xxx at the end of method

    OR

    return xxx in finally block

    Ya, but I think the first way is much better, finally clause not complete all the way. New IDEs warn that as well, may be just a notification. Also more clear when you do in the end of the method your code readers.

  5. #5
    swati.jyoti is offline Member
    Join Date
    Mar 2009
    Posts
    42
    Rep Power
    0

    Default Reply...

    :) Thanks ...

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Try- catch block + return + IO package
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 10:09 AM
  2. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  3. System.exit() in catch block.
    By new_2_java in forum Advanced Java
    Replies: 8
    Last Post: 06-24-2008, 03:45 PM
  4. Try Catch block issues
    By kewlgeye in forum New To Java
    Replies: 11
    Last Post: 04-29-2008, 07:10 AM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •