aspose file tools
The moose likes Java in General and the fly likes Stack over flow error not in try/finally block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Stack over flow error not in try/finally block" Watch "Stack over flow error not in try/finally block" New topic

Stack over flow error not in try/finally block

Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117

Whenever we use recursion (call the method with in the method) we will get stack overflow error after certain stage or number of calls.

But i am not getting any stack over flow error when we call it in try/finally block.

public class Test_Recursion

public static recursionMethod()


recursionMethod(); //if i commet this line i am getting stack overflow error

public static void main(String args[])




Can any one answer?

Thanks in advance.
Sankar. S
David Newton

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

The recursionMethod is missing a return type but we'll assume it's void.
This question is a bit tricky. In both cases you'll get a StackOverflowError (SOE)
but in just 1 case you'll see it.

In the case that the line is commented:
// Empty finally block so throw the error and go up the stack

In the case that the line in not commented:
// 1. Run the finally block --> will cause another SOE
// 2. Go up the stack, goto 1

// The above will loop for a very long time

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40388
You usually only get an overflow error with recursion when there is an error in the recursion and it goes into an endless recursion.
David Newton

Joined: Sep 29, 2008
Posts: 12617

See Campbell's answer.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40388
If you have a void method, as Vouter Oet noticed, is that a true recursion?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yes. In a computer science context recursion is technique in which a method in certain circumstances invokes itself.

Btw. it's Wouter not Vouter
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 40388
Thank you, and sorry for the misspelling.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Hi All,

This is interesting question to me.
Wouter and Campbell already explained that question.
I modified the given code and include wait time to understand what happens when program runs.

See the code below

Case 1 When we don't comment line number 17

Both output are snapshots not a full output.
Its not possible to show full output.
If any one interested in output then make a change in wait time of program by reducing seconds in waitFor(seconds) method

I observed following output of program with case 1

outside try block 2963 call
inside try block 2963 call
outside try block 2964 call
inside try block 2964 call
outside try block 2965 call
inside try block 2965 call
outside try block 2966 call
inside try block 2966 call
outside try block 2967 call
inside try block 2967 call
outside try block 2968 call
inside try block 2968 call
outside try block 2969 call
inside try block 2969 call
outside try block 2970 call
inside try block 2970 call
outside try block 2971 call
inside try block 2971 call
outside try block 2972 call inside finally block 2972 call
outside try block 2973 call inside finally block 2973 call
outside try block 2974 call
inside try block 2974 call
outside try block 2975 call inside finally block 2975 call
outside try block 2976 call inside finally block 2976 call
outside try block 2977 call
inside try block 2977 call
outside try block 2978 call
inside try block 2978 call
outside try block 2979 call inside finally block 2979 call
outside try block 2980 call inside finally block 2980 call
outside try block 2981 call
inside try block 2981 call
outside try block 2982 call inside finally block 2982 call
outside try block 2983 call inside finally block 2983 call
outside try block 2984 call
inside try block 2984 call
outside try block 2985 call
inside try block 2985 call
outside try block 2986 call
inside try block 2986 call
outside try block 2987 call inside finally block 2987 call
outside try block 2988 call inside finally block 2988 call
outside try block 2989 call
inside try block 2989 call
outside try block 2990 call inside finally block 2990 call
outside try block 2991 call inside finally block 2991 call
outside try block 2992 call
inside try block 2992 call
outside try block 2993 call
inside try block 2993 call
outside try block 2994 call inside finally block 2994 call
outside try block 2995 call inside finally block 2995 call
outside try block 2996 call
inside try block 2996 call
outside try block 2997 call inside finally block 2997 call
outside try block 2998 call inside finally block 2998 call
outside try block 2999 call
inside try block 2999 call
outside try block 3000 call
inside try block 3000 call
outside try block 3001 call
inside try block 3001 call
outside try block 3002 call
inside try block 3002 call
outside try block 3003 call inside finally block 3003 call
outside try block 3004 call inside finally block 3004 call
outside try block 3005 call
inside try block 3005 call
outside try block 3006 call inside finally block 3006 call
outside try block 3007 call inside finally block 3007 call
outside try block 3008 call
inside try block 3008 call
outside try block 3009 call
inside try block 3009 call
outside try block 3010 call inside finally block 3010 call
outside try block 3011 call inside finally block 3011 call
outside try block 3012 call
inside try block 3012 call
outside try block 3013 call inside finally block 3013 call
outside try block 3014 call inside finally block 3014 call
outside try block 3015 call
inside try block 3015 call
outside try block 3016 call
inside try block 3016 call
outside try block 3017 call
inside try block 3017 call
outside try block 3018 call inside finally block 3018 call
outside try block 3019 call inside finally block 3019 call
outside try block 3020 call
inside try block 3020 call
outside try block 3021 call inside finally block 3021 call
outside try block 3022 call inside finally block 3022 call
outside try block 3023 call
inside try block 3023 call

Case 2 When we comment line number 17
Following output observerd for case 2
inside finally block 2972 call
inside finally block 2972 call
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(
at sun.nio.cs.SingleByteEncoder.encodeLoop(
at java.nio.charset.CharsetEncoder.encode(
at sun.nio.cs.StreamEncoder.implWrite(
at sun.nio.cs.StreamEncoder.write(
at Test_Recursion.recursionMethod(
at Test_Recursion.recursionMethod(

According to JLS3.0 when try throws error then finally runs if finally throws error then error thrown by try block is ignored and error thrown by finally takes precedence over previous thrown error this happens with case 1 and process is going on infinitely.

According to JLS3.0 when try throws error then finally executes normally so error thrown by try block is not ignored and program ends abnormally by throwing runtime error this happens with case 2.

Kindly note that number shown in output is recursive call depth which is shown by counter variable in program.
StackOverflowError is thrown when recursive call happens 2972 times

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Rob Spoor

Joined: Oct 27, 2005
Posts: 19839

If you do not comment that line, the finally block is executed which starts another infinite run. This will cause another StackOverflowError which causes another infinite run from its finally block. These infinite runs have precedence over throwing the errors. After all, finally always executes, except when you call System.exit.

In short:
1) recursiveMethod
2) recursiveMethod
3) recursiveMethod
4) StackOverflowError, causing the run to end
5) finally block: recursiveMethod
6) recursiveMethod
7) recursiveMethod
8) go to 4)

How To Ask Questions How To Answer Questions
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Hi Rob,
You are perfectly correct. I agree with you.
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
The second case will also terminate with stack overflow error. But only after 2 POWER OF <stack size> execution, as each method call has 2 possibility of going in to recursion and last execution, where all methods have entered into finally, throws exception will returned to JVM.
I agree. Here's the link:
subject: Stack over flow error not in try/finally block