aspose file tools*
The moose likes Java in General and the fly likes repeat the function throws exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "repeat the function throws exception" Watch "repeat the function throws exception" New topic
Author

repeat the function throws exception

Steve Jiang
Ranch Hand

Joined: May 17, 2004
Posts: 107
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

Joined: Aug 05, 2005
Posts: 3646
    
  15
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


Joanne
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
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

Joined: Aug 05, 2005
Posts: 3646
    
  15
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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
And a well named boolean variable makes it much clearer what the code is doing
Ganesh Shirsat
Ranch Hand

Joined: Jan 10, 2010
Posts: 33

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

Joined: Sep 13, 2008
Posts: 12

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

Joined: Feb 11, 2011
Posts: 77
Try @RetryOnFailure annotation.


SCEA, PMP, read my blog: www.yegor256.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: repeat the function throws exception