# How to plot function with pattern-matched argument and catch-all definition that throws

Consider the following function:

f[n_?NumericQ] := n;
f[args___] := Throw[{args}];


Then try to plot it:

Plot[f[x], {x, 1, 10}]


Throw::nocatch: Uncaught Throw[{ChartingPrivatepvar$$7957}] returned to top level. Hold[Throw[{ChartingPrivatepvar$$7957}]]

## Examination

If we now redefine f without the catch-all definition, and examine the behaviour of Plot, we find the following:

Clear[f];
f[n_?NumericQ] := n;
Reap@Plot[f[Sow[x]], {x, 1, 10}]


{-Graphics-, {{1, 1.00018, ChartingPrivatepvar$8374, 1., 1.17666, 1.36818, ..., 9.80849, 9.90733, 9.95675, 9.98147, 9.99382, 1.00276, 9.8054, 9.90424, 9.95367, 9.97838, 9.99073, 9.99691}}} So why does Plot evaluate function with argument ChartingPrivatepvar$..., and moreover why after 2 numerical substitutions?

How can Plot work with a function that has a catch-all definition which throws error if its other patterns aren't matched?

I would like, if possible, to have Plot throw if the pattern checks of f don't match, and Catch it on a higher level.

• Plot[Catch[f[x]], {x, 1, 10}] ? – Daniel Lichtblau Nov 12 '19 at 16:21
• Thank you! You are correct, I forgot to mention I would like, if possible, to have Plot throw if the pattern checks of f don't match, and Catch it on a higher level. If I catch the error inside Plot everything will seem as if it worked right and a plot will be created, even when all of the plot's sampled [x]s evaluate to Throw. – Spyros Thanasoulas Nov 12 '19 at 17:07
• Use f[n_?NumericQ] := n; f[_Symbol] = Null; f[args___] := Throw[{args}]? – Carl Woll Nov 12 '19 at 17:13
• @Carl This seems to be what I'm looking for! Using head Symbol in patterns will do it for me I'm pretty certain, thanks! Any feedback on why that symbol is evaluated as x? Isn't that kind of an odd behaviour or am I missing something? – Spyros Thanasoulas Nov 12 '19 at 18:14