I am running a calculation to generate a dataset. I use several iterations to sweep over a parameter space. A few times an error message is generated (which is not the problem per se). Is it possible to output the value of the iterative variables (or other local variables) for each generation of an error message, without interrupting the evaluation, in order for me to check what is going wrong?

I tried (without success)something like this:

messageHandler = If[Last[#], Print[i]] &;
Internal`AddHandler["Message", messageHandler];

for a code that could be something like:


My code is of course much more complex so that I do not want to use Trace, that outputs a huge amount of data that I cannot read. I tried the debugger in the WorkBench but it seemed like I could not access the local variables after a message interruption.

Anyone has a simple Idea?


1 Answer 1


You can do that with the function Check:

 Check[1/i, Print["i = ", i]], {i, -5, 5}

Mathematica graphics

Here, 1/i takes the place of your complicated code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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