• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with handling error with try/catch block  RSS feed

 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mooses!

I made a Fibonaccis sequences by recursion and I want to find out at which point the RAM ends. Thus I used try/catch block with catching the StackOverflowError but then I'm not able to handle it anyhow.
Below is my code.





when I run it from a main


then I get the correct sequence with last elements:

recursion method ---->
1
2
...
...
104082058052194146
-4233843534399771193
-4129761476347577047
-8363605010747348240
next method ---->

but not any orders following the catch.

Can you give me any hints?

cherse
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got break in the loop? you should not need break in a loop; you should be returning results. You should not need a loop in a recursive method at all.

The reason for the negative numbers is that you have an overflow error. You are also not calculating the numbers correctly; there are no Fibonacci numbers where index ≤ 100 beginning 104…. The reason is that you are not incrementing the “minus” numbers. (And minusX is a very poor name for those parameters.) If you did not get an error message, it means there was no Exception and your method exited normally without running out of stack space.

You can avoid the arithmetic overflow error by using BigIntegers instead of longs.
BigInteger fib1 = BigInteger.ONE; // etc
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I feel compelled to point out that using Exceptions for controlling logical flow is a Really Bad Idea. As the name implies, Exceptions should only occur for exceptional (that is, unexpected or unusual) conditions. They should not be used as part of the normal program flow. There is always a better way to control the flow of your code.

But as Campbell has pointed out, you have some other issues to address first.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for the Exception is to see how far the program will run before it exhausts the available memory or stack space. The answer being, “at least 8001×”.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Przemek Geminski wrote: I want to find out at which point the RAM ends.

I missed that part. I see now that this is more an exercise of curiosity than a practical application.


 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You work out a formula for calculating Fibonacci numbers. You use BigInteger. You start with fib₁ and fib₂, then calculate fib₃ and fib₄, and keep increasing with no base case for your recursion. So you get an infinite recursion and will actually run out of stack space.

Remember that stack space will vary between different releases of Java® and between different machines. Also fib₁ = 1 and fib₂ = 1.
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all replies.

I removed the break commend and I got the response from try/catch (thanks). But, the error shows far below the first negatives apear. So, I'm not sure about its source?


-975011167850518975
5729017722739041026
4754006554888522051
-7963719796081988539
-3209713241193466488
7273311036434096589
The error ocured with 2943element
4063597795240630101
null
-7109835242034824926
-3046237446794194825


Maybe this is not the most elegant way to find the overflow index, but what else can I do?
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the reason of negatives is reaching the limit of long type. I'll try BigInteger but as I glimpse on it it is not so straight to use.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, BigInteger is very easy to use.
Now convert that to a recursive algorithm and you will have something which overflows your stack space.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The largest such Fibonacci number which fits into the bounds of a long is
Fib( 73) = 806515533049393
 
Przemek Geminski
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for this BigIntegers. I used it less elegantly than you, but it works (i hope).
Now it is time to face recursion because as you wrote - somethings went wrong.

best!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!