Tech Off Post

Single Post Permalink

View Thread: Error Handling, use exceptions?
  • User profile image

    Hey spod,

    spod wrote:
    there was a long and interesting argument about this internally. The main reason for not doing

    if( IsParsable( String ) )
       int i = Int.Parse(string );

    is that it's hard to avoid a double parse of the string, which is too much of a perf hit in some cases...

    Bah! I knew you'd say that. I think that the wrong decision was made.

    I'd prefer to have Boolean Int32.IsInvalid(String) and Int32 Int32.Parse(String).

    If you want performance then you wouldn't check for invalid data, you'd assume it was valid and let the exception fall out.

    I guess there is a case for TryParse, although every bone in my body wants to deny it. I imagine this would typically be used in UI code (e.g. to get an Int32 value from a TextBox) in this case the performance hit of double parsing is negligable. If you were trying to get an Int32 value out of a text file or what have you, it would be better (in my opinion) to insist that your data was valid and let the exception fall out (so you wouldn't check at all).

    What are the cases where TryParse(String, ref Int32) significantly helps performance?

    Why not support all methods? I.e. have an IsInvalid/IsValid/IsParseable query, a Parse method that throws and TryParse as is?