This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need logic

 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the problem what i am facing in a large code thats why i elaborate this problem in a small example what i want to do is



value = "abc"; this line(in the try block) may throws or mayn't throws exception(suppose) what i need is when this throws the exception it goes back in the try block and execute this line value = "abc"; again until it throws exception if not throws exception it return the value please guide me as i tried allot but in vain , Regards
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you want a loop, and move the try-catch inside the loop:
 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Looks like you want a loop, and move the try-catch inside the loop:


thanks for reply buddy i knew i need a loop here but the problem is how i can stop the loop when it doesn't throws exception
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try this.

 
akhter wahab
Ranch Hand
Posts: 151
Java MyEclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when it doesn't throw an exception, my code will execute the return statement and leave the loop (and complete method).
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhakar Reddy Bokka wrote:You can try this.



The 'flag' variable is not needed here. As soon as you set it to false the next line returns from the method so the false value will never be checked. Rob's code did exactly what you wanted. If you only wanted to exit the loop and not return from the method, you could replace the return with a break.

You could do it using a flag, but you then wouldn't need the return statement.
 
Stephan van Hulst
Bartender
Posts: 6337
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you *really* don't want to use the return statement, you can test for value == null, rather than a separate flag variable.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except if null is a valid return value of the method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic