Gilad Naaman
Gilad Naaman

Reputation: 6550

C# - failed parse exception?

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);

Upvotes: 16

Views: 36987

Answers (10)

George Johnston
George Johnston

Reputation: 32258

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 value;
if(!int.TryParse("1", out value))
{
    // You caught it without throwing an exception.
}

Upvotes: 36

Alec Sanger
Alec Sanger

Reputation: 4562

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

Upvotes: 11

Nikki9696
Nikki9696

Reputation: 6348

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.

Upvotes: 0

Heinzi
Heinzi

Reputation: 172330

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.

Upvotes: 6

Ashley Grenon
Ashley Grenon

Reputation: 9565

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

Upvotes: 0

PetPaulsen
PetPaulsen

Reputation: 3508

Just try it. This code:

int.Parse("");

Throws a FormatException.

Upvotes: 0

Rob
Rob

Reputation: 45770

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.

Upvotes: 0

Randolpho
Randolpho

Reputation: 56391

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

Upvotes: 2

Achilles
Achilles

Reputation: 11309

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.

Upvotes: 0

Steve Danner
Steve Danner

Reputation: 22158

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

Upvotes: 13

Related Questions