I sometimes see methods in the .net framework prefixed with “Try” e.g. int.TryParse(..).
I assume this means that the method is the same as a int.parse, but wrapped in a try catch?
Does this mean that if I write methods which have a try catch around them (e.g logging, which I never want to raise an exception), they should be prefixed with “try” as well?
Your assumption may be correct, but this is not the meaning of the
Try***type methods.The promise is indeed that the method will no throw an exception (how this is managed internally does not matter) when called.
You will notice that these methods return a boolean indicating whether the
Try***was successful or not.The
TryParsemethods specifically have anoutparameter that will be populated with the result of the parse if successful.To answer the questions directly:
Try***as this is the kind of behavior implied by such a name