Alex Rivera | Logout

C# - failed parse exception?

Asked 2010-07-29T20:30:39.787
16

I am writing a program in C#, and I want to catch exceptions caused by converting "" (null) to int. What is the exception's name?

EDIT: I'm not sure I can show the full code... But I'm sure you don't need the full code, so:

int num1 = Int32.Parse(number1.Text);
int num2 = Int32.Parse(number2.Text);
Edit
Report

1 Answer

0

Convert.ToInt32 does not throw a format exception ("input string is not in the correct format") on a null string. You can use that if it is acceptable for the result to be a 0 for a null string. (still pukes on empty string though)

        string s = null;
        int i = Convert.ToInt32(s);

But if you expect a number to be in the box, you should either use TryParse (as was suggested) or a Validator of some kind to inform the user that they need to enter a number.

answered 2010-07-29T21:36:18.727

Your Answer