CKTIS / Java
StackOverflowError — call stack overflow
The JVM can throw StackOverflowError when a computation needs more stack space than the thread is allowed. Deep recursion is a common cause, but a sufficiently deep nonrecursive call chain can also overflow the stack. The error belongs to the Error hierarchy, not Exception; addressing it usually means fixing the method-call structure rather than catching the error.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
public class StackOverflowError extends VirtualMachineError
public StackOverflowError()
public StackOverflowError(String s)StackOverflowError in java.lang signals that a computation has exceeded the thread’s permitted stack size. Deep recursion is a common cause, but a sufficiently deep nonrecursive call chain can also overflow the stack. It extends VirtualMachineError and indirectly extends Error. It is not a subtype of Exception, so catch (Exception e) does not catch it. As an unchecked error, it does not require a throws declaration.
The StackOverflowError() constructor creates an object without a detail message; StackOverflowError(String s) stores the supplied message. Constructing the object alone neither overflows the stack nor throws it. The example constructs a StackOverflowError object and uses getMessage() to read and print its message (demo). It then evaluates error instanceof Error and prints the result, true, on the second line.
For an actual overflow, check how deep the call chain is and look for cycles of calls between methods. If recursion is involved, inspect its termination condition and consider limiting its depth or replacing it with iteration. Do not make correctness depend on a fixed number of permitted calls: available depth depends on the platform and thread stack. Catching Error should not be a normal control-flow mechanism.
The documentation scope is Java SE 21; the production runtime is not attested. The example was run in an isolated environment, and its standard output matched the displayed output exactly.
Examples
public class Main {
public static void main(String[] args) {
StackOverflowError error = new StackOverflowError("demo");
System.out.println(error.getMessage());
System.out.println(error instanceof Error);
}
}Result
demo true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.