Short answer: No.
A try-catch
structure is pretty expensive in terms of resources. From a design point of view, it feels wrong too; an exception should be just that: an exception - something unusual, probably an error. The way you are using it here is just to check which of two valid formats to use.
In essence, what you are doing here is using exception handling as a computationally expensive if-else
operation.
A better solution might be to try to extract the contents of jObject["caseData"]
first, and check it's format explicitly to know which option to use. Once you know that, you can use a regular if-else
structure to decide how to deserialize it.
Something like:
var dateText = jObject["caseData"].ToString();
var matchingFormat = dateText.Contains("/") ? "d/M/yyyy" : "d-M-yyyy";
caseData.AddRange(
JsonConvert.DeserializeObject<List<CaseInfo>>(
jObject["caseData"].ToString(),
new JsonSerializerSettings { DateFormatString = matchingFormat }));
Note: This assumes you are reasonably sure you can trust that the input will be in one of the two formats. You might want to add some more validation, especially if the date field is something that end-users can manipulate freely.