Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2009, 08:52 AM
Member
 
Join Date: Mar 2009
Posts: 35
Rep Power: 0
swati.jyoti is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-02-2009, 08:58 AM
Senior Member
 
Join Date: Dec 2008
Location: Hong Kong
Posts: 473
Rep Power: 2
mtyoung is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2009, 10:54 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
    String getName() {

        try {
            return br.readLine();
        }
        catch (IOException e){
            e.printStackTrace();
        }
        return "";
    }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-02-2009, 10:58 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-02-2009, 12:24 PM
Member
 
Join Date: Mar 2009
Posts: 35
Rep Power: 0
swati.jyoti is on a distinguished road
Default Reply...
Thanks ...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-02-2009, 03:32 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
If you solve the problem please mark it as solved as well. It's really helpful to all other members, looking into your thread.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Try- catch block + return + IO package vasavi.singh New To Java 1 02-23-2009 11:09 AM
Question reg try/catch block nn12 New To Java 1 09-16-2008 06:56 PM
System.exit() in catch block. new_2_java Advanced Java 8 06-24-2008 04:45 PM
Try Catch block issues kewlgeye New To Java 11 04-29-2008 08:10 AM
try...catch block javaplus New To Java 3 11-06-2007 08:53 PM


All times are GMT +2. The time now is 03:07 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org