StackOverflowError
What is StackOverflowError?
StackOverflowError: The call stack ran out of space — usually caused by infinite recursion (a function that keeps calling itself forever). A StackOverflowError, or "Maximum call stack size exceeded" in JavaScript, means the call stack ran out of space. Each function call adds a frame to the stack, and something kept adding frames without ever returning.
Frequently asked questions about StackOverflowError
How is StackOverflowError used in practice?
The usual cause is recursion without a working stop condition: a function that calls itself forever. Indirect loops cause it too, such as two functions calling each other, or two objects whose methods reference each other while converting to JSON.
What should you keep in mind with StackOverflowError?
The stack trace typically shows the same few function names repeating, which points straight at the loop. Check the base case of recursive functions, and watch for relationships that refer back to each other. For processing deep structures like large trees, an iterative approach with your own list of items to visit avoids the limit entirely.
At a glance
| Category | Error types |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |