Ray Cassick wrote:
I think these methods still catch an exception internally, they just don't expose it to the caller.
I used to think this, but it's quite the opposite actually. Take a look at the Int.TryParse(x) methods in reflector. They both call the same underlying methods (NumberToInt33 and HexNumberToInt32 depending on the string format), which return booleans, and then the difference is that the TryParse methods return true/false based on the result, where as the Parse method actually throws an exception based on the boolean result.
internal static unsafe uint ParseUInt32(string value, NumberStyles options, NumberFormatInfo numfmt)
{
byte* stackBuffer = stackalloc byte[1 * 0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
uint num = 0;
StringToNumber(value, options, ref number, numfmt, false);
if ((options & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!HexNumberToUInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
}
return num;
}
if (!NumberToUInt32(ref number, ref num))
{
throw new OverflowException(Environment.GetResourceString("Overflow_UInt32"));
}
return num;
}
internal static unsafe bool TryParseInt32(string s, NumberStyles style, NumberFormatInfo info, out int result)
{
byte* stackBuffer = stackalloc byte[1 * 0x72];
NumberBuffer number = new NumberBuffer(stackBuffer);
result = 0;
if (!TryStringToNumber(s, style, ref number, info, false))
{
return false;
}
if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
{
if (!HexNumberToInt32(ref number, ref result))
{
return false;
}
}
else if (!NumberToInt32(ref number, ref result))
{
return false;
}
return true;
}
(There are other differences, like one calls TryStringToNumber, but if you follow the stack all the way down, the Try method doesn't ever suppress an exception) I don't know if this is true for all TryXXX methods, but it's the same for the ones I've looked at.
Simon