Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

For[i=1,i<=imax,i++,
 (*code*)
   ]

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You can do that with the function Check:

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

Mathematica graphics

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

share|improve this answer
    
Thanks for your quick answer! –  Felix.k May 18 at 20:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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