Example implementation of 'TryParse' or 'TryGetValue' [closed]
Can you give me an example of implementation of .NET 'try' pattern?
Edit:
I don't mean a "try-catch" statement, I mean a try patterns, like those used in TryParse()
, and TryGetObjectByKey()
methods.
Most specifically what to do with raised exceptions in custom 'try' pattern methods. Should I log it, should I ignore It. Does' anybody know what is the practice with those methods?
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");
}
}
}
I'd expect the implementation to look very much like the "throwing" version in each case - except where there's a "throw new FormatException" statement in the throwing version, there's "return false"... and at the end of the success path, there's an assignment to the output parameter and "return true".
To some extent you can simulate the throwing version from the non-throwing version (although in terms of performance you can't do the reverse) - you can just call TryParse
and throw an exception if it returns false. However, that means the exception won't contain detail about why the parse failed.
The exact amount of code reuse you could accomplish - while still having good performance from the Try version, and detailed exceptions in the throwing version - will depend on the exact task at hand.
I guess you mean something like Int32.TryParse
or Dictionary<TKey, TValue>.TryGetValue
method. Those method exists to evade costly exception throwing, and let the caller know the failure by other means -- boolean return value in this case.
精彩评论