[C#] Applying Try Catch in this Statements
Anyhow know how to apply Try Catch for Invalid FormatException in this statements?
Quote:
do
{
Console.Write("Enter number: ");
test = int.Parse(Console.ReadLine());
if (test <= 0 || test > 999)
{
Console.WriteLine("Test cannot be equal to 0 or greater than 999");
}
}
while (test <= 0 || test > 999)
Re: [C#] Applying Try Catch in this Statements
Code:
bool isValid = false;
do
{
Console.Write("Enter number: ");
try
{
test = int.Parse(Console.ReadLine());
}
catch (FormatException fe) { Console.WriteLine(fe.Message); }
if (test <= 0 || test > 999)
Console.WriteLine("Test cannot be equal to 0 or greater than 999");
else
isValid = true;
}
while (!isValid)
Re: [C#] Applying Try Catch in this Statements
Quote:
Originally Posted by
TakaShi^^
Anyhow know how to apply Try Catch for Invalid FormatException in this statements?
Code:
do
{
Console.Write("Enter number: ");
try {
test = int.Parse(Console.ReadLine());
} catch (InvalidFormatException e)
{
Console.WriteLine("Error!: " + e.ToString());
}