Menu
Carl Camera

Making Exceptions Exceptional

Okay, I'm guilty of some poor programming practices on occasion. Specifically and recently, I used exception processing as a means of input validation. I used the System.Convert.ToInt32(string) method to validate whether an input was numeric or not.

But there goes Scott again, bringing to light my less than stellar habits. So I'll throw this open to the four or five of you listening. Help me create an improved IsInt32() method.

Mine comes in last place:

public bool IsInt32 (string s)
{
   bool bIsInt = true;
   try
   {
      int test = System.Convert.ToInt32(s);
   }
   catch
   {
      bIsInt = false;
   }
   return (bIsInt);
}

So help me out. What's the best way to implement IsInt32() without depending on exception processing?

Note to responders: Start all code lines with four spaces. Markdown will recognize those lines as code. No html escaping necessary.

Comments