Throw a format exception C#
Asked Answered
C

4

5

I'm trying to throw a format exception in the instance someone tries to enter a non-integer character when prompted for their age.

        Console.WriteLine("Your age:");
        age = Int32.Parse(Console.ReadLine());

I'm unfamiliar with C# language and could use help in writing a try catch block for this instance.

Thanks very much.

Columniation answered 23/9, 2012 at 6:19 Comment(4)
Int32.Parse will throw a FormatException if a non-numeric string is passed to it - your code looks like it does what you want it to?Sympathetic
Do you mean you're trying to catch a format exception?Roanne
Int32.Parse could return three different exceptionsAerophobia
this should help you msdn.microsoft.com/en-us/library/ms173160(v=vs.80).aspxZacek
H
24

That code will already throw an FormatException. If you mean you want to catch it, you could write:

Console.WriteLine("Your age:");
string line = Console.ReadLine();
try
{
    age = Int32.Parse(line);
}
catch (FormatException)
{
    Console.WriteLine("{0} is not an integer", line);
    // Return? Loop round? Whatever.
}

However, it would be better to use int.TryParse:

Console.WriteLine("Your age:");
string line = Console.ReadLine();
if (!int.TryParse(line, out age))
{
    Console.WriteLine("{0} is not an integer", line);
    // Whatever
}

This avoids an exception for the fairly unexceptional case of user error.

Hernia answered 23/9, 2012 at 6:22 Comment(2)
I'm a little confused by this line: if (!int.TryParse(line, out age)Columniation
@user1513637: In what way? int.TryParse returns whether or not it was successful, and store the result in the out parameter. See the documentation for details.Hernia
L
3

What about this:

Console.WriteLine("Your age:");
try
{    
     age = Int32.Parse(Console.ReadLine());
}
catch(FormatException e)
{
    MessageBox.Show("You have entered non-numeric characters");
   //Console.WriteLine("You have entered non-numeric characters");
}
Lindblad answered 23/9, 2012 at 6:25 Comment(0)
A
0

No need to have a try catch block for that code:

Console.WriteLine("Your age:");
int age;
if (!Integer.TryParse(Console.ReadLine(), out age))
{
    throw new FormatException();
}
Aerophobia answered 23/9, 2012 at 6:22 Comment(0)
C
-1

you can do this...

int age;

Console.WriteLine("Your age:"); age=Conver.ToInt32(Console.Readline());

//@Erfunll

Coit answered 16/9, 2021 at 16:28 Comment(3)
The original author was asking for help on how to write a try/catch. Your answer might be improved by showing him how to do this.Ashbey
Hi and welcome to Stack Overflow! Please take the tour. Thanks for answering but can you also add an explanation on how your code solves the issue? Also, check the help center for info on how to format code.Corpuscle
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Dialyse

© 2022 - 2024 — McMap. All rights reserved.