5

Which exception would I use in a try/catch to find out when the user has inputted data in the wrong format?

Example:

try { string s = textBox1.Text; // User inputs an int // Input error MessageBox.Show(s); } catch(what exception) { MessageBox.Show("Input in wrong format"); } 

Thanks

2
  • I don't see where the code you've posted is subject to any exception. Assuming there's no catastrophic failure or anything like that, this code will always work as expected. If you can't validate the input, don't use exceptions for that. You can use Exception to catch all exception types. Commented Jul 13, 2011 at 15:44
  • this code shouldn't fail so its a bad example but some common exceptions for converting types is InvalidCastException, FormatException, ArgumentNullException and OverflowException, there are of course others depending on the conversion Commented Jul 13, 2011 at 15:51

4 Answers 4

25

Don't do this. It's a misuse of exception handling. What you are attempting to do is considered coding by exception, which is an anti-pattern.

An exception is exactly what it sounds like, an exception to the norm. It's defined by something you either haven't accounted for, or simply can't account for through traditional validation. In this situation, you can definitely account for a format issue ahead of time. If you know there is a possiblity that the inputted data will be in the wrong format, check for this case first. e.g.

if(!ValidateText(textBox1.text)) // Fake validation method, you'd create. { // The input is wrong. } else { // Normally process. } 
Sign up to request clarification or add additional context in comments.

6 Comments

unexpected input is so normal it's certainly not exceptional.
@Sogger: no, not really. The user can't catch exceptions, so what value is it to throw an exception when the user does something incorrect?
It depends what the application is. Input doesn't necessarily come from a user sitting at the computer, it could be from an external database or other source. Besides, unless you are processing a large volume of inputs, the difference between if/else and try/catch to the actual usefulness of the program is just semantics. I believe a try{parse} is even faster than a tryparse;parse; in the non-error case even for high volume processing. The times I find myself using trycatch is when there is multiple input validations, and then the code is much more readable/maintainable than 10+ ifelse blocks.
@Sogger You're reaching. There's always a hypothetical reason not to do something -- and it's clearly defined here what the OP is attempting to accomplish.
It's not a reach to believe that an application would have multiple inputs or inputs from non-user sources. The only reason I commented was because your answer is speaking in generalized terms, and I wanted to point out for later readers that they aren't a bad person or an 'anti' if they use try-catch.
|
10

You should avoid using Exceptions as flow control.

If you want a textbox to be an int, this is where int.TryParse() method comes in handy

int userInt; if(!TryParse(textBox1.Text, out userInt) { MessageBox.Show("Input in wrong format"); } 

Comments

2

You can go with Exception ex to catch all exceptions. If you want to catch a more specific one, though, you'll need to look at the documentation for whatever functions you are using to check the validity of the input. For example, of you use int.TryParse(), then you will want to catch FormatException among others (see: http://msdn.microsoft.com/en-us/library/b3h1hf19.aspx for more information).

1 Comment

Ehh, I was focusing on the question as asked wrt figuring out which exception to use. But I think George's answer is ultimately the more correct one because the OP definitely should not be using exceptions here.
1

You can create your own exception like ↓

public class FormatException : Exception 

And In your source , it might be...

if (not int) throw new FormatException ("this is a int"); 

Then , In your catch ...

catch(FormatException fex) 

1 Comment

Although technically true, this isn't a good pattern to follow (see the accepted answer)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.