It seemed a reasonable pattern for me to use:
int.TryParse()
and simple if, rather than:
int.Parse()
inside the try block. Till I found this sample in MS pattern & practices
/// <summary>
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
/// </summary>
/// <param name="Key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
try
{
// if new value is different, set the new value.
if (isolatedStore[Key] != value)
{
isolatedStore[Key] = value;
valueChanged = true;
}
}
catch (KeyNotFoundException)
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
catch (ArgumentException)
{
isolatedStore.Add(Key, value);
valueChanged = true;
}
catch (Exception e)
{
Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
}
return valueChanged;
}
Isn't it better to use Contains method insted? On the other hand, I have read that CLR can optimize try-catch to simple goto.