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

share|improve this question

2 Answers 2

up vote 3 down vote accepted
Do[
  Check[{i/i, 1/(i - 5)}, Print@i],
  {i, -10, 10}] // Quiet

0

5

share|improve this answer

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}}}

share|improve this answer

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.