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.
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.
The most common cause of StackOverFlowError is excessively deep or infinite 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With