Per the documentation, String.Format will throw a FormatException if either (A) the format string is invalid or (B) the format string contains an index that cannot be found in the args array.
I want to be able to determine which (if either) of those conditions fail on any arbitrary string and array of arguments.
Is there anything that can do that for me? Thanks!
Follow up to gbogumil’s answer, in the first case you get:
and in the second, you get:
If you need to sense which (for user messaging or logging), then you could use a try catch like qor72 suggested, and check for what the error message starts with. In addition, if you need to capture what the format string was, and what the args were, you will need to do something like this:
EDIT: Added the simple regex to count format tokens. Might help…
Hope this helps. Good luck!