# Caching RecursionLimit::reclim error

I am learning how to catch exceptions, RecursionLimit::reclim in particular. To see how it works, I came up with the following example:

Clear[a]
a = 1
test[] :=
Check[If[a > 100, a, If[RandomReal[1000] < 1, Print["yupeee"];Print[a], test[]]],
a += 1; test[]]
test[]


With variable a I would like to count not how many times Test[] has been called, but how many times recursion limit has been exceeded. When I run it, I either get the output

"yupeee"
1


or

$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>
$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >> General::stop: Further output of$RecursionLimit::reclim will be suppressed during this calculation. >>
1117


I expected something like

"yupeee"
45


or

101


but definitly not

1117


Could someone please explain this strange number? Any idea how can I control the number of exceeded recursion limit? And since I am a beginner, PLEASE, try to make it simple.

Thanks

-
You probably want another a+=1 before that call to test[] in the inner If[]. Without that, the results you are seeing are entirely sensible. –  Daniel Lichtblau Apr 3 '14 at 15:32
The number is approximately 1024 (\$RecursionLimit) + 100 (your condition on a). Change the condition and the number will change along with it. –  R. M. Apr 3 '14 at 15:39
@DanielLichtblau and @rm-rf: Thank you both for your answers. I'm not sure if I understand it right, but with variable a I would like to count not how many times Test[] has been called, but how many times recursion limit has been exceeded. This is why I can't understand how can recursion limit be exceeded more than 100 times. Any idea how can I control the number of exceeded recursion limit? –  Spela Apr 3 '14 at 16:39
When a message is generated inside a nest of Check expressions, every one of those Checks evaluates its fail expression. The inner Check doesn't "absorb" the message if you see what I mean. So in your code when a exceeds 100 you stop creating recursions with your If statement, but this happens inside a nest of ~1000 Check expressions. Therefore a gets incremented a further 1000 times as the message propagates outwards, triggering every Check to evaluate a+=1. For a simpler example define f := Check[f, i++] then evaluate i=1; f –  Simon Woods Apr 4 '14 at 13:10
@SimonWoods: Thanks a lot. I didn't even realize that Check expressions were nested, since there was literally only one Check in the code:) It makes perfect sence now and I am sorry to bother you all with obvious questions. –  Spela Apr 4 '14 at 17:08