This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Recursion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recursion" Watch "Recursion" New topic
Author

Recursion

zain hoda
Greenhorn

Joined: Jul 23, 2011
Posts: 6
How do i implement recursion in a try catch block?

I tried using the THIS keyword but it did not work.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
Individual statements can't be recursive - only entire methods can.


Ping & DNS - updated with new look and Ping home screen widget
zain hoda
Greenhorn

Joined: Jul 23, 2011
Posts: 6
I tried recursion in the entire method, but it seems that due to the try catch block in that method, there seems to be some problem.
Is that possible?
Any way out of it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
It's impossible to tell what problems a method might have the source of which we can't see. In other words: post the source code (and please UseCodeTags when doing so).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
... and welcome to the Ranch
zain hoda
Greenhorn

Joined: Jul 23, 2011
Posts: 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

And what errors do you get?

Please tell the details - if you're only saying "it doesn't work", or "there is some error" then you are making it difficult for people to help you solve your problem.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Line 21 will lead to either an infinite loop or a StackOverflowError; I think it's the latter. The reason is quite clear: you called move with two arguments. An exception occurs. What do you do? You call the method with the same two, unmodified arguments. As a result, the same exception will occur, and you call the method with the same two, unmodified arguments. As a result, the same exception will occur, etc.
Line 29 will also cause the infinite loop or a StackOverflowError, since you're again calling the same method with the same, unmodified arguments.

In recursion, you usually call the same method, but with different arguments. Somewhere in the method it will perform a check that will lead to a path that does not call the same method, thereby ending the recursion.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
zain hoda
Greenhorn

Joined: Jul 23, 2011
Posts: 6
I was able to sort out that problem.
Wat i did was make two variables static, that solved the problem.

Thanks for the help guys
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Which two variables are static? Why did you make them static? That changes the behaviour of the variable, which may cause you problems later.
Changing the design of your class as a workaround because a control structure doesn't work sounds very suspect to me.
 
jQuery in Action, 2nd edition
 
subject: Recursion
 
Similar Threads
java programming
Tech Word Game
Explain to me what is recursion and examples of using it
Printing a String object without using loop
Arrays