1 of 2

# how to print stack trace when TimeConstrained times out

According to the documentation, TimeConstrained generates an interrupt which interrupts the computation. This interrupt is treated just like an abort, in the sense that it respects AbortProtect.

I'd like to intercept this abort/interrupt and write a stack trace to a log file, so that I can determine whether or not the time out resulted from a bug. I tried this:

changeInterrupt[] :=
(
Unprotect[Interrupt];
testInterrupt = True;
Interrupt[args___] :=
(
Print["This is where I would write my stack trace to a log file."];
Block[{testInterrupt = False}, Interrupt[args]]
) /; testInterrupt
Protect[Interrupt];
)

TimeConstrained[changeInterrupt[]; While[True, Null], 1]
changeInterrupt[]; TimeConstrained[While[True, Null], 1]


and also the same thing but with Interrupt replaced with Abort. I get the print statement when I generate an abort/interrupt, but not when either of the TimeConstrained invokations terminate.

Any ideas?