2
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

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.

$\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.