• Post Reply Bookmark Topic Watch Topic
  • New Topic

Try, Catch Error  RSS feed

 
James Newby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to write a catch, try block to ask user to enter number between 1-5 and if number outside these values it would provide error and ask user to input again. The problem this is not correct I get error "missing return statement" any help? thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Walk through the code in your head - take all possible branches - does each path return an int?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the statement that could cause a NumberFormatException to be thrown should be inside the try block. You have it outside right now.
 
James Newby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks so done this now and it seem to compile without problems
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Newbie wrote:ok thanks so done this now and it seem to compile without problems

It may compile but I doubt that it works the way it should. Why did you move the while statement inside the try block? The statement that can throw a NumberFormatException is the parsing of a string to an integer value but I don't see that in your new code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!