var consequence = Take a look at.DivideNumbers(15, 5);
if (consequence.IsSuccess)
Console.WriteLine($"The result's: {consequence.Worth}");
else
Console.WriteLine($"Error occurred: {consequence.ErrorMessage}");
Keep away from exceptions utilizing the Attempt-Parse sample
The Attempt-Parse sample is one other nice technique to keep away from exceptions in your utility. In C#, the Attempt-Parse sample is represented utilizing the TryParse methodology, which converts an information kind into one other and returns a Boolean worth. If the parsing course of succeeds, then the output is true, false in any other case. You’ll be able to reap the benefits of this sample to keep away from exceptions in your code whereas changing knowledge sorts as proven within the code snippet given beneath.
String str = "1000";
Boolean consequence = Int32.TryParse(str, out int n);
if (consequence == true)
Console.WriteLine($"{n}");
else
Console.WriteLine("Error in conversion");
Keep away from exceptions by calling Attempt* strategies
When changing an information kind to a different, it’s best to reap the benefits of the Attempt-Parse sample as proven above. Additional, word that there are different Attempt strategies comparable to TryGetValue. These strategies return false if unsuccessful and return the results of a profitable operation utilizing an out parameter. The next code snippet reveals how this may be completed.