When failure is expected rather than exceptional, there is something cleaner than try: TryParse reports success as a bool.
Program.cs
using System;
class Program
{
static void Main()
{
string input = "42";
if (int.TryParse(input, out int number))
{
Console.WriteLine(number * 2);
}
else
{
Console.WriteLine("Not a number");
}
}
}
Output
84
TryParse returns true or false and, on success, puts the value in the variable declared with out. No exception is thrown at all.
Which to use
TryParse for input you expect to be wrong sometimes — anything a person types. try/catch for the genuinely exceptional.