8
$\begingroup$

I sometimes encounter bugs that involve a recursion limit. It's often very hard to find the origin of the problem.

Would there be a way to copy the stack somewhere and abort the evaluation in order to know what is causing the error when a message such as the following is emitted (this is just an example)?

t = {t}
$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of {t}

In practice I have a complex program with a user interface and I can't know what's triggering the recursion. Also in Wolfram Workbench, the message breakpoint doesn't stop on such message.

$\endgroup$
  • 3
    $\begingroup$ The topic is related but threads linked by Leonid seem to be quite close: 1514 $\endgroup$ – Kuba May 10 '15 at 14:39
  • $\begingroup$ This helps, thanks Kuba $\endgroup$ – faysou May 10 '15 at 15:03
10
$\begingroup$

Using the answer of belisarius in Setting up diagnostic error messages in large Mathematica projects allows to do exactly what I wanted.

$MessagePrePrint = ( #; Print[Stack[_][[;; -5]]]; Abort[]) & 
t = {t} (*prints the stack and aborts*)

Specializing $MessagePrint to only print the Stack in a recursion helps even more for this question.

$MessagePrePrint=
	If[#===$RecursionLimit::reclim2 || #===$IterationLimit::itlim,
            Print@#;
            Quiet@Print[Stack[_][[;;-5]]];
            Abort[];
            ,
            #
        ]&;
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.