The int.TryParse method converts strings into ints. It never throws an exception—even on invalid input and null. It is called in a different way than int.Parse. It is overall preferable to int.Parse in most program contexts.
TryParse
Note:Use int.TryParse to parse integers in most situations. This is often faster and results in simpler logic.
Note 2:This method is ideal for input that is not always correct. When errors occur, performance is better.
int quantity;
try
{
quantity = int.Parse(txtQuantity.Text);
}
catch (FormatException)
{
quantity = 0;
}
catch (OverflowException)
{
quantity = 0;
}