StackOverflowError and the concept of Infinite Recursion

Ademola Kazeem
3 min readOct 31, 2017

--

At first glance of the word StackOverFlow, the first thing that comes to the mind of a typical developer is www.stackoverflow.com, a web community where millions of developers help answer questions asked by other developers in different languages and technologies. In fact, many developers who probably do not code in java might not know what StackOverFlow is really all about.

Some confuse Errors and Exceptions, others are struggling with the differences between Infinite Loop and Infinite Recursion. This post will clear the air about those concepts.

Exceptions

Every programming language has a concept called Exception, Exceptions are ways by which the code a programmer writes complains that something is not right somewhere in the code. In Java, Exceptions inherits from java.lang.Throwable which in turns inherits from java.lang.Object (Just like every class inherits from java.lang.Object).

In Java, there are 3 basic types of Exceptions namely:

  1. Runtime Exception: This is also known as Unchecked Exception. Runtime Exception extends RuntimeException. They are not required to be handled or declared by the JVM or Programmer throws them. A few example include ClassCastException, NullPointerException, ArithmeticException, IllegalArgumentException, etc. This is not our topic of discuss.
  2. Checked Exception: Checked Exceptions as the name implies are checked. Checked Exception is a child class of Exception but not a child class of Runtime Exception and programs are required to handle or declare them. Example includes IOException, FileNotFoundException. We will not go into details of that.
  3. Error: Errors are thrown by JVM and should not be handled or declared. They extend Error class. Examples are: ExceptionInInitializerError, StackOverFlowError and NoClassDefFoundError. The actual topic of discussion is StackOverFlowError.

StackOverflowError

In Java, every time a method is called, all the parameters and local variables are put on the stack. When a method calls itself, it forces java to perform the operations of putting parameters and local variables on the stack too many times to the extent that the stack runs out of room and it eventually overflows. This is known as StackOverflowError.

If you execute the above code, you will get StackOverflowError, the output of the above contains the following lines:

The output of StackOverflowError

This method call never ends, once java realises that there is no more room in the stack, it throws the error. This concept is known as Infinite Recursion.

Infinite Recursion versus Infinite Loop

It is only normal for anyone to confuse Infinite recursion with infinite loop, they are two different concepts in programming: While Infinite recursion might be a better thing to encounter because the JVM throws error once it runs out of room and overflows, Infinite loop means that there is a loop running without control, its a loop without end, is not caught by the JVM, it eventually uses all the CPU resources until it is discovered and killed.

--

--

Ademola Kazeem

Vibrant & Articulate Software Engineer | Technologies Enthusiast | Adventurer. https://www.persistentminds.com