Making a data specific error turn into “Nothing”

This is a tad bit hard for me to describe (and the title doesn't help) but I will try my best to explain my problem.

I have a variable data that changes depending on whatever I put into it. The function I use applied several inputs FUNCTION[#]&/@{1,2,3,4} However, for certain data I create, there isn't a variable for {3,4}. I've written a ReplaceRepeated that transforms the output for {3,4} so that it equals Nothing.

My issue now is that I have to plot this data, and I format it all the same using the following:

MapAt[DateList@*ToString@*First, data[[3]], {All, 1}];
MapAt[DateList@*ToString@*First, data[[4]], {All, 1}];


Is there a ReplaceRepeated function that makes it so whenever there is an error I can somehow make it equal to Nothing? Something along the lines of //.[ERROR] -> Nothing

The question really boils down to, how do I take the error that is produced from

MapAt[DateList@*ToString@*First, data[[4]], {All, 1}];


and make it into Nothing?

EDIT: Regarding what the result is, it looks something like this:

MapAt[DateList@*ToString@*
First, {{{"20170609", 2.03}, {"20170613", 2.28}, {"20170627",
23.32}, {"20170703", 2.83}, {"20170727", 2.92}, {"20170731",
26.35}, {"20170803", 2.22}}, {{"20170613", 2.48}, {"20170703",
1.83}}}[[4]], {All, 1}]


However, the issue is when I change what data is, the values after First, change as well. I originally wanted to do something like this:

MapAt[DateList@*ToString@*First, data[[4]], {All,
1}]//.MapAt[DateList@*ToString@*
First, {{{"20170609", 2.03}, {"20170613", 2.28}, {"20170627",
23.32}, {"20170703", 2.83}, {"20170727", 2.92}, {"20170731",
26.35}, {"20170803", 2.22}}, {{"20170613", 2.48}, {"20170703",
1.83}}}[[4]], {All, 1}] -> Nothing


However, once again,

{{{"20170609", 2.03}, {"20170613", 2.28}, {"20170627",
23.32}, {"20170703", 2.83}, {"20170727", 2.92}, {"20170731",
26.35}, {"20170803", 2.22}}, {{"20170613", 2.48}, {"20170703",
1.83}}}


would change after I change data.

• What is the error? What's in the list after the application of your function for those cases that cause an error? – MarcoB Aug 4 '17 at 15:24
• Hi MarcoB, I've made an edit. – jeff Aug 4 '17 at 15:29
• Does Quiet help with the reporting of the error? – bill s Aug 4 '17 at 15:42
• Sorry if I seemed unclear. It's not as much as getting rid of the error notifications, but rather making the result (which is from my POV an error since it doesn't apply the function to the data correctly) and making the whole thing Nothing. – jeff Aug 4 '17 at 15:44
• You can use Check e.g. MapAt[...,Check[Part[data,4],Nothing],{All,1}]... – N.J.Evans Aug 4 '17 at 15:55

It seems that your problem originates when you try to extract a Part that does not exist. As N.J.Evans writes in a comment you could Check for Part::partw and return Nothing when it appears:

myPart1[expr_, parts__] :=
Quiet[Check[expr[[parts]], Nothing, Part::partw], Part::partw]

myPart1[Range@5, 7]

Nothing


However this method is vulnerable as if we turn Off Part::partw it fails:

Off[Part::partw]

myPart1[Range@5, 7]

{1, 2, 3, 4, 5}[[7]]


If you only need to extract a single element at a time at the first level of the expression then you might explicitly check its Length; this works even with Part::partw off:

myPart2[expr_, p_Integer] := If[Abs[p] > Length[expr], Nothing, expr[[p]]]

myPart2[Range@5, 7]

Nothing


Of course if you can avoid this entirely that is best, and I wonder where e.g. 4 in data[[4]] comes from that leads to this error.

• Thanks for taking the time to comment Mr. Wizard. You're right I did use N.J. Evans method in the end, worked pretty well since I haven't encountered any errors through testing. It was a relatively simple set of data so didn't need to extract a single element at a time at the first level of the expression. Thanks again for the help! – jeff Aug 4 '17 at 19:36