Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cause/force a StackOverflowError for testing

I want to test my server to make sure I am bubbling up and handling some specific errors correctly (by handling I mean cleaning up and shutting down). What is the best/correct way to induce a stack overflow error, is there a standard way of doing this, or is this just ugly and a bad idea. This is something I would like to test.

like image 557
rsavchenko Avatar asked Sep 20 '13 22:09

rsavchenko


People also ask

What is the cause of StackOverflowError?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

Which of the following expressions is most likely to cause a StackOverflowError?

The most common cause of StackOverFlowError is excessively deep or infinite recursion.

What would cause a StackOverflowError to occur in recursion?

The most-common cause of stack overflow is excessively deep or infinite recursion, in which a function calls itself so many times that the space needed to store the variables and information associated with each call is more than can fit on the stack.


1 Answers

You can use an endless recursion:

public void recursiveMethod() {
    recursiveMethod();
}

This will cause a StackOverflowError, as the stack gets continuously filled and never emptied.

IDEOne demo

like image 146
BackSlash Avatar answered Oct 06 '22 01:10

BackSlash