wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 41137
    
  45
Individual statements can't be recursive - only entire methods can.


Ping & DNS - my free Android networking tools app
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: 41137
    
  45
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: 38045
    
  22
... 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: 14074
    
  16

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: 19655
    
  18

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: 38045
    
  22
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.
 
 
subject: Recursion
 
Similar Threads
Tech Word Game
java programming
Printing a String object without using loop
Explain to me what is recursion and examples of using it
Arrays