• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

repeat the function throws exception

 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the function will throw exception when the input is not valid, but I can't change the function since it is generated by third party.

I hope to let the program go only when the user input the correct input. The simple try catch as below only can run limit times for user input.




How could we rerun the function until we get the correct input (with unlimited try input) ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Jiang wrote:How could we rerun the function until we get the correct input (with unlimited try input) ?

Put it in a while loop
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
Steve Jiang wrote:How could we rerun the function until we get the correct input (with unlimited try input) ?

Put it in a while loop

and put a break statement after the function call... if there is an exception., it will never reach the break
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Raje wrote:and put a break statement after the function call

You should avoid using break whenever possible. Far better to just set the condition being tested by your while loop to false.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a matter of opinion. Break, continue and return before the end of the method can all work fine if used intelligently. But I agree that a while (true) in combination with a break inside the loop should be prevented.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a well named boolean variable makes it much clearer what the code is doing
 
Ganesh Shirsat
Ranch Hand
Posts: 33
Google Web Toolkit Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can execute the program first time and on the basis of first execution decide if function is throws exception or not.
if it is throw exception then make a isValidUInput flag to false and program re run till the input is valid.

 
Javedali Shaikh
Greenhorn
Posts: 12
Chrome Eclipse IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throw user defined exception requesting the user to enter valid inputs. Try to display some sample example for valid input to user at first itself. Do post whether your problem has been solved?
 
Yegor Bugayenko
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try @RetryOnFailure annotation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic