When the following code is run:
(function recur() {
recur()
})()
the following error is raised —
Uncaught RangeError: Maximum call stack size exceeded
— with the stack filled with references to the function (recur).
So why does the following code:
(function recur() {
try {
recur()
} catch (error) {
recur()
}
})()
even though the error is caught in the try block, not return the error in the catch block? Or at least complain about the function overflowing the call stack?
When the code is run, it does pause all other non-asynchronous code from executing, but still... no errors?! What's going on here?
EDIT:
This behavior is especially weird with code like this:
(function notRecur() {
try {
Symbol() + 2
} catch (error) {
Symbol() + 2
}
})()
that returns a TypeError when executed.
Just another question to experiment with, thanks for reading through and replying.
RangeErrorthat could get thrown, you create a newcatchhandler. If you're handling everyRangeError, where do you expect an unhandledRangeErrorto come from?RangeErrorwould get thrown in thecatchhandler preventing the script from calling therecurfunction again.RangeErroris thrown in thetryblock and caught in thecatchblock. Once you've caught theRangeError, it's like it never happened. Then, in yourcatchblock, you callrecuragain. This time around, you create a whole newcatchblock to handle the nextRangeErrorthat occurs, etc, etc. You have tonnes of thesecatchhandlers due to the recursion - not many of them will actually see aRangeErroroccur, but when they do, they squash it and continue on with the recursion.Symbol() + 2completely standalone and you'll get aTypeError. There's no recursion involved here - so the error occurs in thetryand is then handled in thecatch. In yourcatch, you generate the same error but there's nocatchfor this as there's no recursive creation ofcatchhandlers.