What is java.lang.StackOverflowError
java.lang.StackOverflowError error indicating that the application stack has been exhausted due to deep recursion, i.e. Your program / script is repeating too deeply.
the details
StackOverflowError extends the VirtualMachineError class which indicates that the JVM has run out or has run out of resources and cannot continue to run. VirtualMachineError which extends the Error class is used to indicate those serious problems that the application should not catch. A method may not report such errors in its throw clause because these errors are abnormal conditions that were never expected.
Example
Minimal, Complete, and Verifiable Example :
package demo; public class StackOverflowErrorExample { public static void main(String[] args) { StackOverflowErrorExample.recursivePrint(1); } public static void recursivePrint(int num) { System.out.println("Number: " + num); if(num == 0) return; else recursivePrint(++num); } }
Console exit
Number: 1 Number: 2 . . . Number: 8645 Number: 8646 Number: 8647Exception in thread "main" java.lang.StackOverflowError at java.io.FileOutputStream.write(Unknown Source) at java.io.BufferedOutputStream.flushBuffer(Unknown Source) at java.io.BufferedOutputStream.flush(Unknown Source) at java.io.PrintStream.write(Unknown Source) at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source) at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source) at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source) at java.io.OutputStreamWriter.flushBuffer(Unknown Source) at java.io.PrintStream.newLine(Unknown Source) at java.io.PrintStream.println(Unknown Source) at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:11) at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:16) . . . at demo.StackOverflowErrorExample.recursivePrint(StackOverflowErrorExample.java:16)
Explaination
When a function call is called by a Java application, a stack frame is allocated on the call stack . stack frame contains the parameters of the called method, its local parameters and the return address of the method. The return address indicates the point of execution from which the execution of the program should continue after the return of the called method. If there is no space for the new stack frame, StackOverflowError a Java Virtual Machine (JVM).
The most common case that can run out of Java application stacks is recursion. In recursion, a method calls itself at runtime. Recursion one of the most powerful general-purpose programming methods, but should be used with care to avoid a StackOverflowError .
Recommendations
DebanjanB Dec 15 '17 at 11:29 2017-12-15 11:29
source share