The error occurs when a function calls itself recursively without a proper termination condition, leading to a stack overflow.