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);
3
  • Why would you want to know the name of the exception? Are you going to catch it? (Hope not...) Commented Jul 29, 2010 at 20:35
  • 9
    Also, note the "" and null are different things. Commented Jul 29, 2010 at 20:46
  • Would checking for false instead of handling an exception be a problem? Commented Jul 29, 2010 at 20:49

10 Answers 10

36

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.
}
Sign up to request clarification or add additional context in comments.

5 Comments

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...
@George, it would be great if you backed up claims like "If you can avoid it, do not code by exception" and "it would be smarter" with some cold facts :-)
@Heinzi First of all - 10ms is a shitload of time in terms of programming. Also, 10ms will accumulate in seconds quite fast. You just need a hundred bad decisions costing you 10ms over the span of few months to slow down your whole app with 1 second. And that's how some websites load every page for 2-5 seconds - because someone (or many people) said "pff, I'm not gonna write 2 more lines to save a few ms" too many times.
@BojidarStanchev: Indeed. In that case you need to compare the lost sales due to the 1 second delay to the additional development and maintenance cost of having all those micro-optimizations in your code. Which one "wins" depends on your business case.
13

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

Comments

11

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.

Comments

6

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.

Comments

2

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

Comments

0

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.

Comments

0

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.

Comments

0

Just try it. This code:

int.Parse("");

Throws a FormatException.

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.