After some searching, I did not find a solution to the following problem, which in my thinking should have some very easy/efficient solution.

I'm looking at a list of images (faces), that I want to conform via FindFaces

faces = 
  ConformImages[HistogramTransform @ ImageTrim[#, FindFaces[#][1]]]& /@ images, 
    ColorSpace -> "Grayscale"]

So far this works like a charm and can also be found here: $\quad \quad $https://reference.wolfram.com/language/example/GenderClassification.html

PROBLEM: The problem is that FindFaces is unable to detect some images and thus creates an error. This error is carried forward into the results list.

GOAL: Essentially I want to simply drop the error from the list. I also need to extract the position of those images to drop corresponding values from a secondary variable.

As the initial problem is not reproducible, here my idea as a very simplified example:

results = Table[1/i, {i, -5, 5}]
posDrop = 6
resultsClean = Delete[results, posDrop]

The 6th value of the list returns the error message "ComplexInfinity". To somehow extract the position (i.e. 6) would suffice, but I'm looking for a solution that does not require me to know the specific error message or, for the image problem, finding non-numeric entries.

  • 3
    $\begingroup$ Seen DeleteCases[]? $\endgroup$ May 27, 2015 at 16:26
  • $\begingroup$ Yes saw it; A) is there a generalized form for "pattern" to identify errors, e.g. sth. like "IsError/IsWarning". B) I need to know which cases had been dropped by the function. Many thanks. $\endgroup$
    – CFW
    May 27, 2015 at 16:35
  • $\begingroup$ If you need to know the position, look at Position. $\endgroup$
    – m_goldberg
    May 27, 2015 at 16:43
  • $\begingroup$ Well, the idea of patterns is that you have to know exactly what you like (or don't, as the case may be). So, something like DeleteCases[list, _DirectedInfinity] will strip out any infinite results in list, since the pattern in the second argument captures how infinities are represented in Mathematica. $\endgroup$ May 27, 2015 at 16:46
  • $\begingroup$ P.S. Cases[] and DeleteCases[] are complementary functions when given the same list and pattern. $\endgroup$ May 27, 2015 at 16:47

1 Answer 1


You can use Check to return a particular value if an error is raised.

(* Here I return the string 'Error' which I can use to find the position of the errors. *)
test[x_] := Check[1/x, "Error"]; 
range = Range[-1,1,1];
ans = test/@range;(*Division by zero will raise a warning.*)
errPos = Position[ans,"Error"];
ans = Delete[ans,errPos];
range = Delete[range,errPos];

Edit: Check actually checks for all printed out messages. If you know you get a particular type of failure from your function, you can specify the message you want Check to respond to. In the case of dividing by zero you'd add Power::infy as the third argument, etc.

  • 3
    $\begingroup$ You can also use Check[1/x, Unevaluated@Sequence[]] to have test delete the item itself: in this case ans would have the desired result. $\endgroup$ May 27, 2015 at 17:48
  • 1
    $\begingroup$ I really like that, I think it'll come in handy! +1 From OP's point of view, it doesn't keep track of which images failed though. Doing something like MapIndexed[Check[{First[#2],f[#1]},Unevaluated@Sequence[]], images], where f[x_]:= whatever image operations you want would probably be perfect. $\endgroup$
    – N.J.Evans
    May 27, 2015 at 18:23

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.