9

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.

faysou
  • 10,999
  • 3
  • 50
  • 125

1 Answers1

10

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[];
            ,
            #
        ]&;
faysou
  • 10,999
  • 3
  • 50
  • 125