1
$\begingroup$

I have long code which goes through iteration process. some times I get error messages. I want to get the iteration numbers (or any kind of output indications) when errors happen. for example, how can I print something when error happen in this simple example:

   Do[{i/i, 1/(i - 5)}, {i, -10, 10}]

here, there are two errors when i=0 & when i=5,

how can I output the values of i when the errors happen.

I have different kinds of errors so I want to get the output if any kind of error happen.

thanks.

$\endgroup$

2 Answers 2

5
$\begingroup$
Do[
  Check[{i/i, 1/(i - 5)}, Print@i],
  {i, -10, 10}] // Quiet

0

5

$\endgroup$
1
$\begingroup$

Another approach:

a = Table[{i/i, 1/(i - 5)}, {i, -5, 5}] // Quiet

{{1, -(1/10)}, {1, -(1/9)}, {1, -(1/8)}, {1, -(1/7)}, {1, -(1/ 6)}, {Indeterminate, -(1/5)}, {1, -(1/4)}, {1, -(1/3)}, {1, -(1/ 2)}, {1, -1}, {1, ComplexInfinity}}

DeleteCases[a, {___, Indeterminate | ComplexInfinity, ___}]

{{1, -(1/10)}, {1, -(1/9)}, {1, -(1/8)}, {1, -(1/7)}, {1, -(1/ 6)}, {1, -(1/4)}, {1, -(1/3)}, {1, -(1/2)}, {1, -1}}

Position[a, #] & /@ {Indeterminate, ComplexInfinity}

{{{6, 1}}, {{11, 2}}}

$\endgroup$

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.