File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Recursion

 
zain hoda
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i implement recursion in a try catch block?

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

 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Individual statements can't be recursive - only entire methods can.
 
zain hoda
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
zain hoda
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zain hoda
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic