Click here to Skip to main content
13,044,428 members (111,708 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How do we handle an exception that occurs when you ask the user to enter an integer, but they instead enter a string which is not a valid integer?
Posted 13-Feb-13 12:08pm
Updated 13-Feb-13 12:21pm
Sergey Alexandrovich Kryukov 13-Feb-13 18:11pm
To start with, the user always enters a string... :-)
All exceptions are handlers in similar ways; not clear what's your problem. Just try to develop this code, and you will see.
You don't have to throw exception, as you can use int.TryParse.
desho 13-Feb-13 18:13pm
i dont get it
AspDotNetDev 13-Feb-13 18:18pm
This is called validation, and there are many ways to handle it. First off, are you making a Windows Forms, ASP.NET Web Forms, ASP.NET MVC, WPF, Silverlight, a console application, or something else?
desho 13-Feb-13 18:20pm
a console application
AspDotNetDev 13-Feb-13 18:22pm
Please update your question with the code that you are using. Make the code sample as small as possible, but fully demonstrate the problem. This will help us give you a quick and complete answer.
desho 13-Feb-13 18:26pm
console.writeLine("plz enter a string");
string s = console.ReadLine();

but the user inter an integer instead.
AspDotNetDev 13-Feb-13 20:08pm
That would not throw an exception. Also, a couple notes to more effectively use Code Project. 1) You can edit your question rather than replying to your question (this allows you to syntax highlight code snippets). 2) If you reply to my comment rather than commenting on your own question, I will get a notification and can respond to you faster.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


The simplest solution is to try to convert the string to an integer, if it works it is an integer if it does not, it is not...

Console.WriteLine("plz enter a string");
string s = Console.ReadLine();
int result;
if (int.TryParse(s, out result))
    Console.WriteLine("you have entered the number: " + s);
    Console.WriteLine(s + " is not a number");

desho 13-Feb-13 19:00pm
Thank u alot
Sergey Alexandrovich Kryukov 13-Feb-13 21:20pm
I recommend you to accept it formally (green button).
valery possoz 13-Feb-13 19:14pm
my pleasure.
Sergey Alexandrovich Kryukov 13-Feb-13 20:29pm
Good, my 4. (Well, all that WriteLine is somewhat redundant)...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can use the regular expression to check whether the entered string literals is a number or contains any string.

string s = "This is string";
string s2 = "10";
string s3 = "10s";
Regex regex = new Regex(@"^\d+$");
bool check = regex.IsMatch(s); // false
check = regex.IsMatch(s2); // true
check = regex.IsMatch(s3); //false

check the return value of IsMatch and prompt the user with error.
desho 13-Feb-13 18:53pm
Error 1 The type or namespace name 'Regex' could not be found (are you missing a using directive or an assembly reference?)
Jibesh 13-Feb-13 18:55pm
Please include the namespace "using System.Text.RegularExpressions;"
desho 13-Feb-13 19:00pm
Thanks alot
Sergey Alexandrovich Kryukov 13-Feb-13 20:27pm
Sorry, I think this is not an effective method. For example, the string may represent some number bigger them *.MaximumValue.
The only reliable and effective method is TryParse.
Jibesh 13-Feb-13 20:56pm
SA can you give an example string??
Sergey Alexandrovich Kryukov 13-Feb-13 21:19pm
For example, UInt64.MaximumValue is 18446744073709551615, so "18446744073709551615" is valid, but, say" "18446744073709551785" is not. And how are you going to find the difference?
Only by UInt64.TryParse or UInt64.Parse with try-catch (by the way, did you see source code of them? different algorythms!). But if you do it anyway, no need for your Regex.
I did not vote, but this is not a solution...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100