Here is an example of using a TryXxx method:
string s = Console.ReadLine();
int x;
if (int.TryParse(s, out x))
{
Console.WriteLine("You entered the valid integer {0}", x);
}
else
{
Console.WriteLine("Invalid input!");
}
Here is an example of defining the method:
bool TryParse(string s, out int x) // out parameter for result
{
if (!allCharactersAreValid(s))
{
x = 0;
return false;
}
// More checks...
// Parse the string...
x = 42;
return true;
}
Exception handling
Most specificly what to do with rised exceptions in custom 'try' pattern methods
Your method probably should avoid throwing any exceptions - if your user wanted exceptions they would use the non-Try version. You should therefore try to avoid calling methods which can throw when implementing your TryXxx. However some exceptions are unavoidable and could be thrown out of your control - for example OutOfMemoryException, StackOverflowException, etc... There is nothing you can do about this and you should not try to catch these exceptions, just let them propagate to the caller. Don't swallow them, don't log them - that's the caller's responsibility.
An example of this is Dictionary<TKey, TValue>.TryGetValue when the key object provided to this method throws an exception when GetHashCode is called. Then the resulting exception is not caught inside the TryGetValue method - the caller will see the exception. This code demonstrates this happening:
using System;
using System.Collections.Generic;
class Foo
{
public override int GetHashCode()
{
throw new NotImplementedException();
}
}
class Program
{
public static void Main()
{
Dictionary<object, object> d = new Dictionary<object, object>();
d["bar"] = 42;
object s;
Foo foo = new Foo();
if (d.TryGetValue(foo, out s)) // results in NotImplementedException
{
Console.WriteLine("found");
}
}
}