Skip to content

RecursionError

Built-in class · Extends RuntimeError

A RecursionError is what Emerald raises when functions call each other so deeply that it would run out of room to keep track of them, which usually means a function that calls itself and never reaches a case that stops. Emerald allows at least 1,000 calls to be active at once, and stops the program with this error instead of crashing.

func forever(n: Int): Int {
return forever(n + 1)
}
try {
forever(0)
}
catch error: RecursionError {
print(error.message)
}
Output
too much recursion calling `forever`

A RecursionError is a RuntimeError, so catch error: RuntimeError catches it too. It is almost always a mistake in the function rather than something to recover from, and the usual fix is a case that stops the recursion. See Functions.

message: String

What went wrong, in words, naming the function that was being called.

The program stops and shows the message with the calls that led there. A long run of the same call is summarized rather than printed a thousand times:

func forever(n: Int): Int {
return forever(n + 1)
}
print(forever(0))
Output
error.em:2:12: RecursionError: too much recursion calling `forever`
return forever(n + 1)
^^^^^^^^^^^^^^
Emerald supports at least 1,000 active calls. Check that the recursion has a case that stops it.
in `forever`, called at error.em:2:12 (999 times)
in `forever`, called at error.em:5:7