wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes need logic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need logic" Watch "need logic" New topic
Author

need logic

akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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


Start Earning Online||Start Earning Using Java
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Looks like you want a loop, and move the try-catch inside the loop:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

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

Joined: Jul 26, 2005
Posts: 189

You can try this.



SCJP 5, SCWCD 5
akhter wahab
Ranch Hand

Joined: Mar 02, 2009
Posts: 151

Thanks Guys
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Aug 05, 2005
Posts: 3419
    
  12
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.


Joanne
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

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

Joined: Oct 27, 2005
Posts: 19651
    
  18

Except if null is a valid return value of the method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need logic
 
Similar Threads
Return Statement from a Method
Exceptions - Question
Question #5 from Doug's Book
exception
try, catch,finally