Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Can try/finally Prevent a StackOverflowError?

DZone's Guide to

Can try/finally Prevent a StackOverflowError?

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

 This post is taken from a popular answer to a question try-finally block prevents StackOverflowError

Question

Take a look at the following two methods:
public static void foo(){
try{
        foo();
}finally{
        foo();
}}

public static void bar(){
    bar();}

Running bar() clearly results in a StackOverflowError, but running foo() does not (the program just seems to run indefinitely). Why is that?

Answer

It doesn't run forever. Each stack overflow causes the code to move to the finally block. The problem is that it will take a really, really long time. The order of time is O(2^N) where N is the maximum stack depth.
Imagine the maximum depth is 5
foo() calls
    foo() calls
       foo() calls
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally calls
       foo() calls
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally calls
    foo() calls
       foo() calls
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally calls
       foo() calls
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()finally
           foo() calls
              foo() which fails to call foo()finally calls
              foo() which fails to call foo()
To work each level into the finally block take twice as long an the stack depth could be 10,000 or more. If you can make 10,000,000 calls per second, this will take 10^3003 seconds or longer than the age of the universe.

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Peter Lawrey, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}