tags:

views:

152

answers:

11

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 here 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);
+8  A: 

You are going to get a FormatException if a parse fails. Why not use int.TryParse instead?

Steve Danner
+6  A: 

As a side not, a simple way to find out the exception is to run it. When you encounter the error, it'll give you the exception name.

Eclyps19
+11  A: 

If you can avoid it, do not code by exception!

The exception name you are looking for is called a FormatException.

However, it would be smarter to first do a TryParse on the object you are attempting to parse, e.g.

int i;
if(!int.TryParse(i,out value))
{
    // You caught it without throwing an exception.
}
George
Why not "if(!String.IsNullOrEmpty(i))" first? Seriously, you don't want to even incur the TryParse. Even if you do it a thousand times and only fail once, the if statement before the TryParse will be faster.
@devinb: Add two extra lines of code just to make it 10ms faster? Sounds like unnecessary micro-optimization to me...
Heinzi
A: 

When the exception fires you can see it's type. The smart thing to do is handle that case and display a graceful message to your user if possible.

Achilles
+1  A: 

Depends on what you're using to do the conversion. For example, int.Parse will throw ArgumentNullException, FormatException, or OverflowException. Odds are it's ArgumentNullException you're looking for, but if that's an empty string rather than a null reference, it's probably going to be FormatException

Randolpho
A: 

You're probably looking to get a System.InvalidCastException, although I think that'll depend on how you try to perform the conversion.

That said, wouldn't it be quicker/easier to simply write the code and try it yourself? Particularly as you haven't specified how you'll be performing the conversion.

Rob
A: 

Just try it. This code:

int.Parse("");

Throws a FormatException.

PetPaulsen
A: 

Exceptions are expensive. You should use int.TryParse. It will return the boolean false if the conversion fails.

townsean
+1  A: 

@George and @steve-danner have the right idea, because you'll get an ArgumentNullException when the string is null, and a FormatException when the string is empty. TryParse() is much more elegant (though not part of NETCF :( )

kbrimington
A: 

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.

Nikki9696
+1  A: 

Let's have a look at the documentation (which is a much cleaner solution that "trying it out"):

public static int Parse(string s)

[...]

Exceptions:

  • ArgumentNullException: s is null.
  • FormatException: s is not in the correct format.

This should answer your question. As others have already mentioned, maybe you are asking the wrong question and want to use Int32.TryParse instead.

Heinzi
+1 for documentation link.
Brian